[Android]PopupWindow 点击外部区域无法关闭的问题
来源:互联网 发布:淘宝网上卖中草药违法 编辑:程序博客网 时间:2024/05/23 22:56
[TOC]
在android4.0/5.0系统上,使用popupWindow时,点击内容外部区域无法关闭,但是在6.0机子上又是正常的,而我在代码中明明已经进行了如下设置:
mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);// 点击其他地方消失mPopupWindow.setOutsideTouchable(true);
google了一下都说要添加一句:
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
测试了果然ok,不过还是想知道下所以然,稍微探究一下:
既然是点击没效果,搜索一下touch事件好了:
在4.0/5.0的 PopupWindow.java
源码中发现是在 PopupViewContainer
类中设置的,而在6.0源码中则是位于 PopupDecorView
类;
继续查找在哪里初始化的,发现 preparePopup()
,它是在显示(showAtLocation()
, showAsDropDown()
)的时候调用的,而 preparePopup()
的具体内容:
// PopupWindow.java @ api 14/19/21private void preparePopup(WindowManager.LayoutParams p) { if (mContentView == null || mContext == null || mWindowManager == null) { throw new IllegalStateException("You must specify a valid content view by " + "calling setContentView() before attempting to show the popup."); } if (mBackground != null) { final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams(); int height = ViewGroup.LayoutParams.MATCH_PARENT; if (layoutParams != null && layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) { height = ViewGroup.LayoutParams.WRAP_CONTENT; } // when a background is available, we embed the content view // within another view that owns the background drawable PopupViewContainer popupViewContainer = new PopupViewContainer(mContext); PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, height ); popupViewContainer.setBackgroundDrawable(mBackground); popupViewContainer.addView(mContentView, listParams); mPopupView = popupViewContainer; } else { mPopupView = mContentView; } mPopupWidth = p.width; mPopupHeight = p.height;}
这里可以看到当background不为null的时候,在contentView外面套了一层PopupViewContainer,而PopupViewContainer中才有关于touch,key的监听事件,因此若未设置背景,则点击外部区域无法取消popupwindow,即使设置了:
mPopupWindow.setOutsideTouchable(true);
而在android6.0中:
// PopupWindow.java @ api 23private void preparePopup(WindowManager.LayoutParams p) { ...... // When a background is available, we embed the content view within // another view that owns the background drawable. if (mBackground != null) { mBackgroundView = createBackgroundView(mContentView); mBackgroundView.setBackground(mBackground); } else { mBackgroundView = mContentView; } // 无论background是否为空,都会创建decorView,它是PopupDecorView的实例,其实就是一个FrameLayout,里面有touch事件的监听,因此无需设置背景也可以点击外部区域取消popupwindow的 mDecorView = createDecorView(mBackgroundView); ......}
总之,跟网友说的一致,添加背景图片就可以了:
Bitmap bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.bg_popup_window);Drawable drawable = new BitmapDrawable(getContext().getResources(), bmp);// 不带参的方法已经deprecatedpopupWindow.setBackgroundDrawable(drawable);
1 0
- [Android]PopupWindow 点击外部区域无法关闭的问题
- 点击popupwindow外部区域,点击事件的响应问题
- 关于PopupWindow 点击外部不能关闭的问题
- Android Dialog 点击对话框外部区域不关闭的设置
- 点击PopupWindow 外部区域消失
- PopupWindow点击外部区域消失
- 自定义Dialog点击弹框外的区域无法关闭问题
- PopupWindow在点击外部区域的时候消失(二)
- PopupWindow点击外部区域不能消失的解决办法
- Android Popupwindow 点击外部消失的实现
- PopupWindow点击外部区域消失(二)
- PopupWindow点击外部区域消失(三)
- android popupwindow 中listview 无法点击问题
- android的popupwindow点击返回按钮关闭
- android的popupwindow点击返回按钮关闭
- Android Dialog 点击对话框外部区域不关闭的设置,设置view.
- Android之点击对话框(AlertDialog.Builder)外部区域不关闭对话框的设置
- PopupWindow中ListView无法点击的问题
- sd acm省赛(c-e
- 切图的那点事儿
- Mac配置hosts文件来访问局限网站
- 自定义进度条
- bzoj 1037 生日聚会 party
- [Android]PopupWindow 点击外部区域无法关闭的问题
- 负载均衡算法
- ie 浏览器js关闭窗口弹出警告
- 在PowerPoint中实现表格的动态填充
- 新浪搜狐网易等那些老牌互联网公司现在都怎样了?
- 排序算法之堆排序
- 读《React Native 用 JavaScript 开发移动应用》,没读几页
- ubuntu下eclipse提示框黑色背景修改
- qt与数据库操作