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);
// 注意代码的顺序

文章最早发布于我的微信公众号 Android开发者家园 中,欢迎大家扫描下面二维码关注微信公众获取更多知识内容。
本文为sydMobile原创文章,可以随意转载,但请务必注明出处!

欢迎大家关注我的微信公众号,和我交流分享

个人创建了一个微信群,里面有行业的大佬,会不断的分享一些优质文章,交流技术,谈天说地。想要加入的可以添加我好友,拉你进群。早点找到组织,困惑会在交流中消失!

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值