設置Dialog全屏,背景使用Dialog佈局顏色

通過搜索引擎找了不少答案,但是沒有一個能解決我的問題,大多數人的需求都是去掉黑色背景,然後只能自己嘗試。
最後通過代碼來設置實現了我需要的效果。

<style name="dialog" >
  <item name="android:windowNoTitle">true</item>
</style>

// style需要自己寫,只用消除title屬性就夠了,也不用設置parent
Dialog dialog = new Dialog(context,R.style.dialog);
dialog.setContentView(R.layout.dialog);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();  
// 設置寬高爲match_parent,不要去算出來屏幕寬高再賦值哦,因爲有些
// 有虛擬按鍵的手機上計算出來的高度不一定準確,所以dialog不會全屏
params.width = WindowManager.LayoutParams.MATCH_PARENT;  
params.heigth = WindowManager.LayoutParams.MATCH_PARENT;  
dialog.getWindow().setAttributes(params);
// 設置dialog距屏幕的邊距都爲0
dialog.getWindow().getDecorView().setPadding(0,0,0,0);
dialog.show();

需要背景是什麼顏色,就在dialog的xml根佈局中設置吧O(∩_∩)O!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章