美文网首页
PopupWindow背景变暗(背景半透明)(两种写法)(适配A

PopupWindow背景变暗(背景半透明)(两种写法)(适配A

作者: Jo_wsj | 来源:发表于2020-11-24 16:34 被阅读0次

1、第一种方案:

    // 设置屏幕透明度
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = act.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0~1.0
        act.getWindow().setAttributes(lp); //act 是上下文context
        //Android10 以后需要加上 否则无效
        act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }
    //设置背景颜色变暗
    private void addBackground() {
        backgroundAlpha(0.4f);
        mPopupWindow.setOnDismissListener(() -> {
            backgroundAlpha(1f);
        });
    }

2、第二种方案:

-第一步 布局文件

<RelativeLayout  
    android:layout_width="match_parent"             
    android:layout_height="match_parent">
                <!--省略代码 -->
        <!--黑色背景遮罩层,平时隐藏-->
        <!--背景bg_transparent 就是一张半透明的图片-->
        <View  
            android:id="@+id/view_transparent" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:visibility="gone" 
          android:background="@drawable/bg_transparent"/>
</RelativeLayout>

-第二步 activity里在弹出popupWindow的的时候让其显示,在关闭popupWindow的时候让其隐藏就可以了

private View transparent;
         transparent = findViewById(R.id.view_transparent);
         //在弹出popupWindow的的时候让其显示
         transparent.setVisibility(View.VISIBLE);
         //在关闭popupWindow的时候让其隐藏就可以了
         transparent.setVisibility(View.GONE);

相关文章

网友评论

      本文标题:PopupWindow背景变暗(背景半透明)(两种写法)(适配A

      本文链接:https://www.haomeiwen.com/subject/mraiiktx.html