1、首先在res/values/styles中自定义dialog属性
<style name="MyDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
2、在dialog的构造函数中super(context,R.style.mydialog)
3在oncreate中动态设置dialog的布局为全屏并且增加背景黑度
if (this.getWindow() != null) {
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.dimAmount = 0.7f;
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
this.getWindow().setAttributes(lp);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
4复写dialog的show()方法,主要作用是焦点失能和焦点恢复,保证在弹出dialog时不会弹出虚拟按键且事件不会穿透。
public void show() {
if (this.getWindow() != null) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
super.show();
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
}
网友评论