设置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!

猜你喜欢

转载自blog.csdn.net/mengliluohua_151/article/details/51734206
今日推荐