您的位置:首页 > 运维架构

PopupWindow 点击后如何消失

2012-09-27 17:25 330 查看
This is because the popup window does not respond to onTouch or onKey events unless it has a background that != null. Checkout some code I wrote to help with this. In the basic case you can to call
PopupWindow#setBackgroundDrawable(new
BitmapDrawable())
 to force it to act the way you expect. You won't need your own onKey listener. You might also need to call
PopupWindow#setOutsideTouchable(true)
 ifyou want it to go away when the user clicks outside of the window boundaries.Extended esoteric answer:The reason the background cannot be null is because of what happens in
PopupWindow#preparePopup
.If it detects 
background
!= null
 it creates an instance of
PopupViewContainer
 andcalls 
setBackgroundDrawable
 onthat and puts your content view in it.
PopupViewContainer
 isbasically a 
FrameLayout
 thatlistens for touch events and the
KeyEvent.KEYCODE_BACK
 eventto 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 on 
PopupWindow
 tohandle that, extend your root 
ViewGroup
 tobehave the way you want.
PopupWindow pw;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)findViewById(R.id.linlay_weight_popup));
pw = new PopupWindow(layout,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setOutsideTouchable(true);
pw.showAsDropDown(btnSelectWeight);
from: http://stackoverflow.com/questions/3121232/android-popup-window-dismissal

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息