PopupWindow点击灰色背景关闭窗口的问题

PopupWindow可以通过WindowManager.LayoutParams来设置窗口背景的不透明度

private void changeBackground(Float alpha){
     WindowManager.LayoutParams lp = getWindow().getAttributes();
     lp.alpha = alpha;
     getWindow().setAttributes(lp);
}

为了实现点击灰色背景关闭窗口的功能有以下两种实现方法:

方法一:让PopupWindow的子布局充满整个屏幕,然后对此布局实现setOnTouchListener事件进行处理关闭该窗口。

  • 实现弹出PopupWindow窗口的方法:
private void showPopupWindow(View viewLayout){
        View popView = View.inflate(this, R.layout.activity_popu_layout, null);
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        if(mPopupWindow == null){
            mPopupWindow = new PopupWindow(popView, display.getWidth(), display.getHeight(), true);
        }
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.alpha = 0.6f;
        getWindow().setAttributes(layoutParams);
        mPopupWindow.showAsDropDown(viewLayout);
        popView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                closePopupWindow();
                return false;
            }
        });
    }
  • 关闭PopupWindow窗口:
private void closePopupWindow() {
        if(mPopupWindow != null && mPopupWindow.isShowing()){
            mPopupWindow.dismiss();
            mPopupWindow = null;
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.alpha = 1f;
            getWindow().setAttributes(layoutParams);
        }
    }

方法二:让PopupWindow的窗口布局自适应,然后并对窗口设置一些属性

  • 实现弹出PopupWindow窗口的方法:
private void showPopupWindow(View viewLayout){
        View popView = View.inflate(this, R.layout.activity_popu_layout, null);
        if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(this);
            mPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
            mPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
            mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            //点击外部区域关闭窗口
            mPopupWindow.setOutsideTouchable(true);
            //获得焦点,true时点击事件不会向上传递由Activity处理
            mPopupWindow.setFocusable(true);
        }
        mPopupWindow.setContentView(popView);
        mPopupWindow.showAtLocation(viewLayout, Gravity.RIGHT | Gravity.TOP, 0
                ,IS_IMMERSIVE_STATUS_BAR ? viewLayout.getHeight() : viewLayout.getHeight() + StatusBarHeightUtil.getStatusBarHeight(this));
        mPopupWindow.update();
        changeBackground(0.6f);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                changeBackground(1f);
            }
        });
    }

mPopupWindow.setFocusable(true);//为true时点击区域外不会触发底下activity的相关点击事件

若实现点击区域外隐藏popupwindow必须设置下面两个属性:

mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//实现点击区域外隐藏popupwindow还可以响应back键隐藏popupwindow
mPopupWindow.setOutsideTouchable(true);//点击区域外可以关闭窗口
PopupWindow点击灰色背景关闭窗口的问题_第1张图片
效果.png

github链接地址-PopupWindowDemo

你可能感兴趣的:(PopupWindow点击灰色背景关闭窗口的问题)