Dialog 寬度占據(jù)全屏
關(guān)于如何自定義設(shè)置 Dialog 的大小,以及如何讓寬度占滿整個屏幕,其實是一個老生常談的內(nèi)容了,特別是對于很多新手來說。關(guān)于這方面的內(nèi)容網(wǎng)上一搜一大把。我也看了一下,大多數(shù)是互相抄襲。來來回回就是那么幾句代碼。真實的運行結(jié)果往往并不是占滿屏幕。這篇文章是把很多常見的情況都舉例了。我們先看 Dialog 占滿屏的效果,好了下面一步一步看,如果不想看過程可以直接跳過看總結(jié)。

正常顯示全屏
一般的設(shè)置寬度占據(jù)全屏的效果
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 設(shè)置成了點擊外部,Dialog 消失。當你點擊 Dialog 周圍時的時候,Dialog 不消失,說明 Dialog 窗口還包含了周圍的一點空間。

![效果圖]

上圖:可以看到 Dialog 的最底層 View 其實是 FrameLayout (也就是 DecorView。默認是有 padding 的,大小為 42,可以通過 mWindow.getDecorView().getPaddingTop() 來獲取。如果給 DecorView設(shè)置一個背景顏色那么就更加明顯了)
正常顯示全屏-DecorView 設(shè)置背景色
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);

設(shè)置了背景色后就很明顯了,周圍的一圈綠色就是最底層 DecorView 的 padding 。所以 Dialog 設(shè)置成了點擊外部,Dialog 消失。當你點擊 Dialog 周圍時的時候,Dialog 不消失。
正常顯示全屏-DecorView 設(shè)置背景色和最小寬度
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 設(shè)置背景色和最小寬度和 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 設(shè)置背景顏色和 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 設(shè)置最小寬度和 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);

[圖片上傳失敗...(image-246532-1540983998175)]

[圖片上傳失敗...(image-46f0c1-1540983998175)]
就像上面這個圖片一樣,其實紅色框就是 TextView ,有一部分沒有顯示出來。
正常顯示全屏-DecorView 設(shè)置最小寬度
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);

[圖片上傳失敗...(image-98e5b4-1540983998175)]
正常顯示全屏-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);
結(jié)果和設(shè)置最小寬度和 padding 為 0 是一樣的

[圖片上傳失敗...(image-db924a-1540983998175)]
總結(jié)
其實要想設(shè)置 Dialog 寬度占滿全屏很簡單,掌握了原理就可以了。原理分析:通過上面的實驗,我們可以了解到一個 Dialog 布局,最底層是 DecorView 這個底層布局是有一個默認的 padding 的,并且它有默認大小,寬度并不是占滿屏的。這就導(dǎo)致了我們單獨設(shè)置了 Window 的 LayoutParams 為 MATCH_PARENT 的時候始終是不能占據(jù)滿屏的。這是因為 DecorView 的大小限制了 Window。因此我們需要在設(shè)置 Window 大小的前面,設(shè)置 DecorView 的大小 window.getDecorView().setLayoutParams(new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,500)); 你也可以這樣設(shè)置,但是這樣設(shè)置實際上是占滿屏了,但是別忘了 DecorView 的 padding ,這樣最外面一圈始終有 padding 看上去的效果就是還是沒有占滿屏。
所有最簡單的方法還是
Window window = dialogMyAddress.getWindow();
// 把 DecorView 的默認 padding 取消,同時 DecorView 的默認大小也會取消
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
// 設(shè)置寬度
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
// 給 DecorView 設(shè)置背景顏色,很重要,不然導(dǎo)致 Dialog 內(nèi)容顯示不全,有一部分內(nèi)容會充當 padding,上面例子有舉出
window.getDecorView().setBackgroundColor(Color.GREEN);
DialogUtils.show(dialogMyAddress);
// 注意代碼的順序