20161128
遇到一个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);
ok了。