setOutsideTouchable
setOutsideTouchable(true),点击非PopupWindow视图区域,直接隐藏PopupWindow。调用setOutsideTouchable(true)会使得点击非PopupWindow视图区域,PopupWindow能够接收到ACTION_OUTSIDE事件,接收到这个事件后执行dismiss。但是在5.0以下需要调用setBackground给PopupWindow设置一个非空的背景才会起作用。
小于5.0分析
5.0以下的PopupWindow,当mBackground为非null时,才会创建PopupViewContainer来封装contentView,而ACTION_OUTSIDE的处理正是由其来处理。点击查看4.4源代码
private void preparePopup(WindowManager.LayoutParams p) {
......
if (mBackground != null) {
......
PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
......
mPopupView = popupViewContainer;
} else {
mPopupView = mContentView;
}
......
}
priv

本文详细探讨了PopupWindow的setOutsideTouchable和setFocusable属性。setOutsideTouchable(true)在5.0以下需要配合非空背景才能使点击外部区域隐藏PopupWindow;5.0及以上版本则无需背景。setFocusable(true)确保按Back键时只隐藏PopupWindow而不退出Activity。了解这些细节对于优化PopupWindow的行为至关重要。
最低0.47元/天 解锁文章
3万+

被折叠的 条评论
为什么被折叠?



