我发现您可以在可绘制的 xml 中设置对话框的边距和填充
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(r.drawable.your_drawable);
dialog.getWindow().setContentView(R.layout.your_custom_dialog_layout);
dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
dialog.show();
看看 r.drawable.your_drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list>
<item
xmlns:android="http://schemas.android.com/apk/res/android"
android:left="70dp"
android:right="70dp">
<shape android:shape="rectangle" >
<solid android:color="@color/white" />
<corners android:radius="4dp" />
<padding
android:bottom="70dip"
android:left="70dip"
android:right="70dip"
android:top="70dip" />
</shape>
</item>
</layer-list>
通过设置项目 android:right/left 来设置drawable的边距。使用形状填充,您可以确保 custom_dialog_layout 中的字段不超过对话框背景的宽度。
我希望它对你有一点帮助。