設定AlertDialog的佔滿螢幕
阿新 • • 發佈:2018-12-14
相信肯定有不少人在Android開發中使用AlertDialog的時候,遇到AlertDialog總是有邊距的問題。下面就來介紹如何使AlertDialog沒有邊距。(只以設定AlertDialog的寬度佔滿螢幕寬度為例,高度情況類似,請自行實現)
先上效果圖:
有人可能會說:還是右邊距呀,哈哈,大哥往下看。。。
左右兩邊有邊距是因為AlertDialog依賴的父佈局設定的有左右邊距分別為10dp,所以顯示的dialog才會有左右邊距。這裡可能有人會說,不想讓dialog有任何邊距的顯示,我想到的方法有兩個:
1:將AlertDialog的父佈局的左右邊距設定為0dp
2:獲取當前手機螢幕的寬度,如下設定:
其中設定:decorView.setBackground();// 這個方法必須設定,裡邊的資原始檔是設定dialog的背景,後面貼出
背景資源的程式碼:
以上兩種方法,可供選擇;下面貼出我寫的一個AlertDialog的程式碼,可做工具類使用,佈局可自由傳入,比較靈活: