弹出PopupWindow后的阴影部分

本文介绍两种在Android应用中实现PopupWindow背景遮罩的方法。第一种方法通过添加一个透明度较低的LinearLayout来作为遮罩,并通过监听外部点击来控制PopupWindow的显示与隐藏。第二种方法则是调整PopupWindow显示时的背景透明度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:

在PopupWindow依附的主界面的布局文件里面加上    

<LinearLayout
        android:id="@+id/cover"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.3"
        android:background="#000000"
        android:orientation="horizontal"
        android:visibility="gone" />


PopupWindow里面不要设置this.setFocusable(true);设置这个PopupWindow关闭的时候我们的LinearLayout黑影不消失,我们可以手动关闭

Popupwindow.setOutsideTouchable(true);
Popupwindow.setTouchInterceptor(new OnTouchListener() {


@Override
public boolean onTouch(View arg0, MotionEvent event) {


if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Popupwindow.dismiss();
mCover.setVisibility(View.GONE);
return true;
}


return false;
}
});


@Override
public boolean onBackPressed() {
if(Popupwindow.isShowing()){
Popupwindow.dismiss();
mCover.setVisibility(View.GONE);
return true;
}

}

不设置this.setFocusable(true);是针对不需要弹出软键盘的情况



方法二:

 // 设置背景颜色变暗(使用这个做黑影)
 WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
 lp.alpha = 0.7f;
 mActivity.getWindow().setAttributes(lp);

可以设置this.setFocusable(true);可以弹出键盘



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值