Android 11中Dialog全屏设置无效问题求助
解决Dialog无法消除屏幕间距的问题
仅设置宽高为MATCH_PARENT无法彻底消除间距,因为Dialog默认自带窗口装饰、背景内边距等属性,需要额外处理这些默认配置:
- 清除窗口默认背景,消除背景自带的间距:
window.setBackgroundDrawable(null); - 移除窗口标题栏(若不需要标题):
window.requestFeature(Window.FEATURE_NO_TITLE); - 清除DecorView的内边距,确保Dialog边缘与屏幕完全贴合:
window.getDecorView().setPadding(0, 0, 0, 0);
将这些步骤和你原有的代码结合,完整示例如下:
Window window = getWindow(); if (window != null) { WindowManager.LayoutParams layoutParams = window.getAttributes(); // 设置宽高匹配屏幕 layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; // 清除默认背景 window.setBackgroundDrawable(null); // 移除标题栏 window.requestFeature(Window.FEATURE_NO_TITLE); // 清除内边距 window.getDecorView().setPadding(0, 0, 0, 0); // 应用属性设置 window.setAttributes(layoutParams); }
如果不需要完全全屏,仅需消除间距,可保留自定义宽高设置,只执行清除背景和内边距的步骤即可。
内容的提问来源于stack exchange,提问作者RobinP2000
相关产品推荐
相关产品推荐

