android关于自定义Dialog中布局match_parent 属性 失效的问题

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值