源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-12-15 08:13 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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);
进行页面操作及用户提示,一切显示正常,如图: [img]http://files.jb51.net/file_images/article/201611/201611271539501.jpg[/img] 当按下屏幕电源按钮,再次点亮屏幕,发现Dialog半透明的灰暗背景消失了..... [img]http://files.jb51.net/file_images/article/201611/201611271539512.jpg[/img] 解决方法:设置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半透明背景消失,希望对大家有所帮助,如果大家有任何疑问,欢迎给我留言,小编会及时回复大家的!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部