Android开发中Dialog半透明背景消失
时间:2021-02-01 09:58:06|栏目:Android代码|点击: 次
近日,遇到一个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半透明背景消失,希望对大家有所帮助,如果大家有任何疑问,欢迎给我留言,小编会及时回复大家的!