Android开发中Dialog半透明背景消失

2019/7/7 21:08:20

本文主要是介绍Android开发中Dialog半透明背景消失,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

近日,遇到一个Dialog半透明背景消失的问题,背景需求是自定义Dialog实现警告提示框:

// 初始化警告弹出框 
alertDialog = new EmpAlertView(context, Utils.getIdByName(context, "style", "alert_style")); 
alertDialog.setCanceledOnTouchOutside(false); 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
layout = inflater.inflate(Utils.getIdByName(context, "layout", "alertview"), null); 
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
// 设置半透明背景 
Window window = alertDialog.getWindow(); 
WindowManager.LayoutParams lp = window.getAttributes(); 
lp.alpha = 0.9f; 
window.setAttributes(lp); 
alertDialog.setContentView(layout);

进行页面操作及用户提示,一切显示正常,如图:

当按下屏幕电源按钮,再次点亮屏幕,发现Dialog半透明的灰暗背景消失了.....

解决方法:设置window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);修改后如下:

// 初始化警告弹出框 
alertDialog = new EmpAlertView(context, Utils.getIdByName(context, "style", "alert_style")); 
alertDialog.setCanceledOnTouchOutside(false); 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
layout = inflater.inflate(Utils.getIdByName(context, "layout", "alertview"), null); 
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
Window window = alertDialog.getWindow(); 
WindowManager.LayoutParams lp = window.getAttributes(); 
lp.alpha = 0.9f; 
window.setAttributes(lp); 
// 防止按下再重新开启屏幕电源,原先变暗的背景变白色 
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
alertDialog.setContentView(layout);

以上所述是小编给大家介绍的Android开发中Dialog半透明背景消失,希望对大家有所帮助,如果大家有任何疑问,欢迎给我留言,小编会及时回复大家的!



这篇关于Android开发中Dialog半透明背景消失的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程