首先要给popwindow设置背景图片为透明,然后让它点击外面触发这个事件为true。
有时设置窗口占满上下屏幕或者左右屏幕。往往点击外部设置就会被消费掉,不生效。
以此设置规范:popwindow设置属性
其中,setBackgroundDrawable();很重要
popupWindow.setBackgroundDrawable(getDrawable());//设置背景透明以便点击外部消失 popupWindow.setOutsideTouchable(true);//点击外部收起 popupWindow.setFocusable(true);//设置焦点生效
getDrawabale方法:这样基本上可以实现外部点击消失。亲测有效/** * 生成一个 透明的背景图片 * @return */ private Drawable getDrawable(){ ShapeDrawable bgdrawable =new ShapeDrawable(new OvalShape()); bgdrawable.getPaint().setColor(MainActivity.this.getResources().getColor(android.R.color.transparent)); return bgdrawable; }