dialog 左右无法全屏

Dialog 宽度占据全屏

关于如何自定义设置 Dialog 的大小,以及如何让宽度占满整个屏幕,其实是一个老生常谈的内容了,特别是对于很多新手来说。关于这方面的内容网上一搜一大把。我也看了一下,大多数是互相抄袭。来来回回就是那么几句代码。真实的运行结果往往并不是占满屏幕。这篇文章是把很多常见的情况都举例了。我们先看 Dialog 占满屏的效果,好了下面一步一步看,如果不想看过程可以直接跳过看总结。



正常显示全屏

一般的设置宽度占据全屏的效果

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
// window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);       //window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
//window.getDecorView().setBackgroundColor(Color.GREEN);

 

Dialog 设置成了点击外部,Dialog 消失。当你点击 Dialog 周围时的时候,Dialog 不消失,说明 Dialog 窗口还包含了周围的一点空间。

 

上图:可以看到 Dialog 的最底层 View 其实是 FrameLayout (也就是 DecorView。默认是有 padding 的,大小为 42,可以通过 mWindow.getDecorView().getPaddingTop() 来获取。如果给 DecorView设置一个背景颜色那么就更加明显了)
正常显示全屏-DecorView 设置背景色

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
// window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);       //window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
window.getDecorView().setBackgroundColor(Color.GREEN);

 

设置了背景色后就很明显了,周围的一圈绿色就是最底层 DecorView 的 padding 。所以 Dialog 设置成了点击外部,Dialog 消失。当你点击 Dialog 周围时的时候,Dialog 不消失。
正常显示全屏-DecorView 设置背景色和最小宽度

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
//window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.horizontalMargin = 0;
window.setAttributes(layoutParams);
window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
window.getDecorView().setBackgroundColor(Color.GREEN);

 


正常显示全屏-DecorView 设置背景色和最小宽度和 padding 为 0

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.horizontalMargin = 0;
window.setAttributes(layoutParams);
window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
window.getDecorView().setBackgroundColor(Color.GREEN);

这个时候是真正的全屏了。
正常显示全屏-DecorView 设置背景颜色和 padding 为 0

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.horizontalMargin = 0;
window.setAttributes(layoutParams);
//window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
window.getDecorView().setBackgroundColor(Color.GREEN);


正常显示全屏-DecorView 设置最小宽度和 padding 为 0

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.horizontalMargin = 0;
window.setAttributes(layoutParams);
window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
//window.getDecorView().setBackgroundColor(Color.GREEN);

就像上面这个图片一样,其实红色框就是 TextView ,有一部分没有显示出来。
正常显示全屏-DecorView 设置最小宽度

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
//window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.horizontalMargin = 0;
window.setAttributes(layoutParams);
window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
//window.getDecorView().setBackgroundColor(Color.GREEN);


正常显示全屏-DecorView padding 为 0

DialogUtils.show(dialogMyAddress, Gravity.BOTTOM);
Window window = dialogMyAddress.getWindow();
Window window1 = getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.horizontalMargin = 0;
window.setAttributes(layoutParams);
//window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
//window.getDecorView().setBackgroundColor(Color.GREEN);

结果和设置最小宽度和 padding 为 0 是一样的


总结

其实要想设置 Dialog 宽度占满全屏很简单,掌握了原理就可以了。原理分析:通过上面的实验,我们可以了解到一个 Dialog 布局,最底层是 DecorView 这个底层布局是有一个默认的 padding 的,并且它有默认大小,宽度并不是占满屏的。这就导致了我们单独设置了 Window 的 LayoutParams 为 MATCH_PARENT 的时候始终是不能占据满屏的。这是因为 DecorView 的大小限制了 Window。因此我们需要在设置 Window 大小的前面,设置 DecorView 的大小 window.getDecorView().setLayoutParams(new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,500)); 你也可以这样设置,但是这样设置实际上是占满屏了,但是别忘了 DecorView 的 padding ,这样最外面一圈始终有 padding 看上去的效果就是还是没有占满屏。

所有最简单的方法还是

Window window = dialogMyAddress.getWindow();
// 把 DecorView 的默认 padding 取消,同时 DecorView 的默认大小也会取消
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
// 设置宽度
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
// 给 DecorView 设置背景颜色,很重要,不然导致 Dialog 内容显示不全,有一部分内容会充当 padding,上面例子有举出
window.getDecorView().setBackgroundColor(Color.GREEN);
DialogUtils.show(dialogMyAddress);
// 注意代码的顺序
---------------------
作者:sydMobile
来源:优快云
原文:https://blog.youkuaiyun.com/sydmobile/article/details/83588708

### 自定义 Element UI `el-dialog` 弹窗样式 为了实现对Element UI中的`el-dialog`弹窗样式的自定义,可以通过覆盖默认CSS类来调整外观。具体来说: 对于希望改变对话框头部样式的情况,可以利用特定的选择器进行样式重写。例如设置`.el-dialog__header`的内边距为15px上下和20px左右[^2]。 如果意图更改关闭按钮的位置或是其视觉效果,则需进一步定制`.el-dialog__headerbtn`以及关联的状态变化样式。这包括但不限于设定顶部偏移量、右侧间距、字体大小等属性,并且当鼠标悬停于关闭图标上时,通过`:hover`伪类改变颜色以增强交互体验[^3]。 下面给出一段用于修改上述提到部分样式的代码实例: ```css /* 调整对话框头 */ .el-dialog__header { padding: 15px 20px 15px; } /* 设置关闭按钮位置及基础样式 */ .el-dialog__headerbtn{ top: 15px; } /* 定义全屏模式下的额外控制项布局 */ .el-dialog__headerfull { padding-right: 30px; position: absolute; right: 20px; top: 15px; font-size: 16px; } /* 配置全屏切换图标的初始状态 */ .el-dialog__headerfull i { color: #909399; cursor: pointer; } /* 当用户将指针移到全屏切换图标上方时的颜色变换 */ .el-dialog__headerfull i:hover { color: #0a76a4; } ``` 此外,在某些场景下可能还需要更深入地介入HTML结构内部来进行更为精细的设计工作。此时建议先查阅官方文档了解组件的具体DOM构成,再基于此做出相应改动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值