PopupWindow背景變暗(背景半透明)(兩種寫法)(適配Android10)

1、第一種方案:

    // 設(shè)置屏幕透明度
    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);
    }
    //設(shè)置背景顏色變暗
    private void addBackground() {
        backgroundAlpha(0.4f);
        mPopupWindow.setOnDismissListener(() -> {
            backgroundAlpha(1f);
        });
    }

2、第二種方案:

-第一步 布局文件

<RelativeLayout  
    android:layout_width="match_parent"             
    android:layout_height="match_parent">
                <!--省略代碼 -->
        <!--黑色背景遮罩層,平時(shí)隱藏-->
        <!--背景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的的時(shí)候讓其顯示,在關(guān)閉popupWindow的時(shí)候讓其隱藏就可以了

private View transparent;
         transparent = findViewById(R.id.view_transparent);
         //在彈出popupWindow的的時(shí)候讓其顯示
         transparent.setVisibility(View.VISIBLE);
         //在關(guān)閉popupWindow的時(shí)候讓其隱藏就可以了
         transparent.setVisibility(View.GONE);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容