dialog如果没有设置style,那么系统会主动设置一个style,这个style中的decorview会存在padding,所以导致match_parent无效
1.方法一
dialog.show();//在show之后修改,必须这样否则无效,没看源码具体原因不知道
Window window =dialog.getWindow();
if (window == null) {
return;
}
window.getDecorView().setPadding(0, 0, 0, 0);
window.getDecorView().setBackgroundColor(Color.WHITE);
//在家里测试时,未设置window的背景无效,在公司时不加也没问题
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.width = getContext().getResources().getDisplayMetrics().widthPixels;
attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(attributes);
2.方法二--------定义自定义style
<style name="MyDialogStyle"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">true</item> </style>