Android之Dialog半透明背景消失
2016-11-26 15:17
323 查看
近日,遇到一个Dialog半透明背景消失的问题,背景需求是自定义Dialog实现警告提示框:
进行页面操作及用户提示,一切显示正常,如图:
![](https://img-blog.csdn.net/20161126145805136?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
当按下屏幕电源按钮,再次点亮屏幕,发现Dialog半透明的灰暗背景消失了.....
![](https://img-blog.csdn.net/20161126145857995?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
解决方法:设置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); 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(背景图等)
- android 自定义Dialog(背景图等)
- android 自定义dialog弹出和消失动画
- android布局设置Button或者ImageButton的背景透明 半透明 透明
- android dialog 背景透明的样式
- 使android dialog点击按钮dialog不消失的方法
- Android ProgressDialog 延时消失
- android基础知识复习(二)——RelativeLayout布局属性、背景、半透明设置(XML设置)
- android 半透明背景
- android点击AlertDialog上的按钮后让其不消失
- Android背景透明的 Dialog
- 如何使Android dialog弹出后当前的 activity背景不变暗
- Android中自定义Activity和Dialog的位置大小背景和透明度等
- android 对话框Dialog背景透明
- Android AlertDialog解决点击按钮立即消失的问题
- Android背景透明的 Dialog
- android 设置Button或者ImageButton的背景透明 半透明
- Android alertdialog的按钮点击后不消失
- Android随记------Dialog自定义背景等样式