前言:
项目开发中遇到关于PopupWindow适配性的问题,特别是在华为手机上显示就会出现类似的情况。
下面这段PopupWindow弹出时背景变暗效果的代码在网上随处可见:
private void setBackgroundAlpha(float bgAlpha){
WindowManager.LayoutParams layoutParams = MainActivity.this.getWindow().getAttributes();
layoutParams.alpha = bgAlpha; //0.0-1.0
MainActivity.this.getWindow().setAttributes(layoutParams);
}
解决方法:
在相关的PopupWindow方法中加入这个方法,问题完美解决。
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//多加这一句,问题就解决了!这句的官方文档解释是:让窗口背景后面的任何东西变暗
private void setBackgroundAlpha(float bgAlpha){
WindowManager.LayoutParams layoutParams = MainActivity.this.getWindow().getAttributes();
layoutParams.alpha = bgAlpha; //0.0-1.0
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
MainActivity.this.getWindow().setAttributes(layoutParams);
}
“`