问题:查看是否这样的设计思路:用一个按钮,通过isShowing()来判断状态,然后展示和隐藏popup。且使能了外部触摸
setBackgroundDrawable(new BitmapDrawable());
setOutsideTouchable(true)
然后在按钮中获取isShowing()的状态,结果一直返回的都是false。
原因分析:你触摸按钮的时候,它属于外部区域,触摸它肯定会把popup隐藏,所以,在按钮事件中判断一直都是false
解决办法:让popup把焦点从外部抢夺过来,setFocusable(true)
即可,至于setTouchable(true)
,不用也行。这个时候也不用判断状态了,按钮中只处理显示代码就行了
附上PopupWindow常用API:
// 一、构造方法:
// PopupWindow 必须设置宽高,无论在构造中,还是后续再添加
PopupWindow (Context context);
PopupWindow(View contentView);
PopupWindow(View contentView, int width, int height);
PopupWindow(View contentView, int width, int height, boolean focusable);
// 二、常用方法:
setWidth(int width); // 设置宽度
setHeight(int height); // 设置高度
setContentView(View contentView); // 设置内容控件
setFocusable(boolean focusable); // 设置是否获取焦点
setTouchable(boolean touchable); // 设置是否能否触摸
setBackgroundDrawable(Drawable background); // 设置背景
setOutsideTouchable(boolean touchable); // 要使能外部触摸,必须设置setBackgroundDrawable()
// 相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor);
// 相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上
showAsDropDown(View anchor, int xoff, int yoff);
// 相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y);
dismiss(); // 隐藏
isShowing(); // 获取显示状态
API参考:
http://blog.youkuaiyun.com/harvic880925/article/details/49272285