android popupWindow 背景遮罩设置最佳方式
2019-08-19 本文已影响0人
XX杰
设置popupwindow的高度是 wrap_parent
重写 showAsDropDown 方法,设置window的背景透明度
@Override
public void showAsDropDown(View anchor) {
if (activity != null && !activity.isFinishing()) {
Window dialogWindow = activity.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.alpha = 0.7f;
dialogWindow.setAttributes(lp);
}
super.showAsDropDown(anchor);
}
重写 dismiss 方法,恢复window的背景透明度
@Override
public void dismiss() {
if (activity != null && !activity.isFinishing()) {
Window dialogWindow = activity.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.alpha = 1.0f;
dialogWindow.setAttributes(lp);
}
super.dismiss();
}
注意 super方法要在 改变透明度的后面
否则会把你的壁纸给 闪出来