自定义布局的Dialog 继承 Dialog
xml布局的时候往往根布局指定固定的宽高会有留白问题,
通过设置其style样式中的 windowNoTitle 为 true 可解决,不需要再根据 LayoutParams 来指定大小(主要是不方便)
代码如下
public class DefaultDialog extends Dialog { public DefaultDialog(@NonNull Context context) { this(context,0); } public DefaultDialog(@NonNull Context context, int themeResId) { super(context,R.style.defaultDialogStyle );//通过第二个参数指定其Style } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_default); } }
样式
<style name="defaultDialogStyle" parent = "@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
设置Dialog 宽度 match_parent
style 加 <item name="android:windowBackground">@color/transparent</item>
onCreate 方法中
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);