关于 PopupWindow 响应 Back 按键收起
项目中遇到一个Bug:PopupWindow 不响应 Back 键收回了。
google了一下,发现解决方法是:PopupWindow#setBackgroundDrawable(new BitmapDrawable())
具体原因是:
The reason the background cannot be null is because of what happens in
PopupWindow#preparePopup. If it detectsbackground != nullit creates an instance ofPopupViewContainerand callssetBackgroundDrawableon that and puts your content view in it.PopupViewContaineris basically aFrameLayoutthat listens for touch events and theKeyEvent.KEYCODE_BACKevent to dismiss the window. If background == null, it doesn't do any of that and just uses your content view. You can, as an alternative to depending onPopupWindowto handle that, extend your rootViewGroupto behave the way you want.
翻译一下就是:在 PopupWindow 初始化的时候会监测是否设置了背景。如果已设置的话,会创建一个 PopupViewContainer (类似于 FrameLayout)来监听 Back 按键。但如果没有设置背景,就不会创建 PopupViewContainer,因此无监听。当然,也可以利用 PopupWindow 的 root ViewGroup 来处理监听。

浙公网安备 33010602011771号