popupwindow 增加半透明遮罩

 

//在show的时候加入addmask

 

private void addMask(IBinder token) {

WindowManager.LayoutParams wl = new WindowManager.LayoutParams();

wl.width = WindowManager.LayoutParams.MATCH_PARENT;

wl.height = WindowManager.LayoutParams.MATCH_PARENT;

wl.format = PixelFormat.TRANSLUCENT;//不设置这个弹出框的透明遮罩显示为黑色

wl.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;//该Type描述的是形成的窗口的层级关系

wl.token = token;//获取当前Activity中的View中的token,来依附Activity

maskView = new View(context);

maskView.setBackgroundColor(0x7f000000);

maskView.setFitsSystemWindows(false);

maskView.setOnKeyListener(new View.OnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

removeMask();

return true;

}

return false;

}

});

/**

* 通过WindowManager的addView方法创建View,产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。

* 比如创建系统顶级窗口,实现悬浮窗口效果!

*/

windowManager.addView(maskView, wl);

}

 

public void removeMask() {

if (null != maskView) {

windowManager.removeViewImmediate(maskView);

maskView = null;

}

}

 

 

 

//记得监听popupwindow消失时还原activity,

window.setOnDismissListener(new PopupWindow.OnDismissListener() {

@Override

public void onDismiss() {

removeMask();

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码来实现弹出PopupWindow,并添加背景遮罩以及点击遮罩时弹窗消失。 首先,在你的布局文件(例如activity_main.xml)中添加一个透明的背景遮罩视图: ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Your main layout content here --> <View android:id="@+id/background_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" /> </FrameLayout> ``` 然后,在你的Activity或Fragment中的代码中,添加以下方法来显示和隐藏PopupWindow: ```java public class MainActivity extends AppCompatActivity { private PopupWindow popupWindow; private View backgroundView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); backgroundView = findViewById(R.id.background_view); // 设置背景遮罩点击事件 backgroundView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismissPopupWindow(); } }); // 显示PopupWindow showPopupWindow(); } private void showPopupWindow() { // 创建PopupWindow视图 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View popupView = inflater.inflate(R.layout.popup_window, null); // 设置PopupWindow的属性 popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示PopupWindow popupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0); } private void dismissPopupWindow() { // 隐藏PopupWindow if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } } } ``` 最后,创建一个popup_window.xml资源文件来定义PopupWindow的布局: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" android:padding="16dp"> <!-- Your PopupWindow content here --> </LinearLayout> ``` 这样,当你的Activity或Fragment启动时,PopupWindow将显示在屏幕中央,并且点击背景遮罩PopupWindow将被隐藏。请根据你的需要修改布局和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值