您的位置:首页 > 移动开发 > Android开发

Android之Dialog半透明背景消失

2016-11-26 15:17 323 查看
     近日,遇到一个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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: