PopupWindow弹窗,弹窗外部不可点击

PopupWindow弹窗,弹窗外部不可点击_第1张图片 弹窗展示
Activity.java中代码
//PopupWindow弹窗
private void showNoneEffect() {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vPopupWindow = inflater.inflate(R.layout.popupwindow, null, false);//引入弹窗布局
    popupWindow = new PopupWindow(vPopupWindow, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
    //点击外部弹出不消失
    popupWindow.setFocusable(false);
    popupWindow.setOutsideTouchable(false);
    //设置透明背景布局
    setTransparentBg();

    popu_sj_vip = vPopupWindow.findViewById(R.id.popu_sj_vip);
    popu_sj_vip.setOnClickListener(this);

    //引入依附的布局
    View parentView = LayoutInflater.from(this).inflate(R.layout.activity_setting, null);
    //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
    popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
}
private void setTransparentBg() {
    // 设置背景颜色变暗
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = 0.7f;//调节透明度
    getWindow().setAttributes(lp);
    //监听弹窗
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            //弹窗关闭  dismiss()时恢复原样
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 1f;
            getWindow().setAttributes(lp);
        }
    });
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    //拦截弹窗外部点击事件
    if (popupWindow != null && popupWindow.isShowing()) {
        return false;
    }
    return super.dispatchTouchEvent(ev);
}

弹窗布局文件popupwindow.xml




    
    

    
        
    




 

你可能感兴趣的:(随记,android,popupWindow,PopopWindow)