PopupWindow不设置背景,弹窗不消失,但是事件向下传递

设置了PopupWindow的background,点击Back键或者点击弹窗的外部区域,弹窗就会dismiss.

  相反,如果不设置PopupWindow的background,那么点击back键和点击弹窗的外部区域,弹窗是不会消失的.

 

  那么,如果我想要一个效果,点击外部区域,弹窗不消失,但是点击事件会向下面的activity传递,比如下面是一个WebView,我想点击里面的链接等. 

  研究了半天,说是要给Window设置一个Flag,
  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
  
  看了源码,这个Flag的设置与否是由一个叫mNotTouchModal的字段控制,但是设置该字段的set方法被标记为@hide
  所以要通过反射的方法调用:
复制代码
   /**
     * Set whether this window is touch modal or if outside touches will be sent
     * to
     * other windows behind it.
     *
     */
    public static void setPopupWindowTouchModal(PopupWindow popupWindow,
            boolean touchModal) {
        if (null == popupWindow) {
            return;
        }
        Method method;
        try {

            method = PopupWindow.class.getDeclaredMethod("setTouchModal",
                    boolean.class);
            method.setAccessible(true);
            method.invoke(popupWindow, touchModal);

        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
复制代码

  然后在程序中:  

UIUtils.setPopupWindowTouchModal(popupWindow, false);

  该popupWindow外部的事件就可以传递给下面的Activity了。

要实现在弹出PopupWindow时添加一个背景遮罩,并在弹窗消失时移除背景遮罩,而不影响活动布局中其他控件的点击事件,你可以使用以下代码: 首先,在你的布局文件中添加一个FrameLayout作为根布局,并设置一个透明的背景遮罩视图: ```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 activity layout content here --> <View android:id="@+id/background_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:clickable="true" android:focusable="true" android:visibility="gone" /> </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); // 显示背景遮罩 backgroundView.setVisibility(View.VISIBLE); } private void dismissPopupWindow() { // 隐藏PopupWindow if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } // 隐藏背景遮罩 backgroundView.setVisibility(View.GONE); } } ``` 这样,当你的Activity或Fragment启动时,PopupWindow将显示在屏幕中央,并添加一个背景遮罩。当点击背景遮罩时,PopupWindow将被隐藏,并且背景遮罩也会消失。其他活动布局中的控件的点击事件不会受到影响。请根据你的需要修改布局和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值