android 调用popupwindow时activity变透明

1.一般情况下,调用popupwindow从当前activity透过去看到上一层的activity,造成2个activity重叠的效果。
解决方法: 设置浮动层的背景变暗

 private void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
        
    }

2.当需要滑动返回上一层界面的时候发现背景是黑的,完全不是需要的效果。
解决方法:需要结合dimAmount的值来设置浮动层的背景亮度。dimAmount取值范围:0.0f~1.0f。0.0f完全不暗,即背景是可见的 ,1.0f时候,背景全部变黑暗。
官方文档:https://developer.android.google.cn/reference/android/view/WindowManager.LayoutParams#dimAmount

//设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha,float dimAmount) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        lp.dimAmount=dimAmount;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

猜你喜欢

转载自blog.csdn.net/baidu_21345205/article/details/84983493
今日推荐