1. 程式人生 > >Dialog 寬度佔滿全屏

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原創文章,可以隨意轉載,但請務必註明出處!

歡迎大家關注我的微信公眾號,和我交流分享

個人建立了一個微信群,裡面有行業的大佬,會不斷的分享一些優質文章,交流技術,談天說地。想要加入的可以新增我好友,拉你進群。早點找到組織,困惑會在交流中消失!