PopupWindow学习总结
经过一段时间的学习,了解了PopupWindow的基本用法,如下:
1、为PopupWindow的view布局,通过LayoutInflator获取布局的view.如:
LayoutInflater inflater =(LayoutInflater)
this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View textEntryView =
inflater.inflate(R.layout.paopao_alert_dialog, null);
2、显示位置,可以有很多方式设置显示方式
pop.showAtLocation(findViewById(R.id.ll2), Gravity.LEFT, 0, -90);
3、进出场动画
pop.setAnimationStyle(R.style.PopupAnimation);
4、点击PopupWindow区域外部,PopupWindow消失
this.window = new PopupWindow(anchor.getContext());
this.window.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() ==MotionEvent.ACTION_OUTSIDE) { BetterPopupWindow.this.window.dismiss(); return true; } return false; } });
教训:
1、 PopuWindow 的大小由下面代码控制;
newPopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
2、 popuWindow.showAsDropDown(v);方法是将PopuWindow显示在View v的左下方; 3、 需要顺利让PopUpWindow dimiss(即点击PopuWindow之外的地方此或者back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在
popuWindow.showAsDropDown(v);或者其它的显示PopuWindow方法之前设置它的背景不为空:如下面两行代码:
ColorDrawablecd = new ColorDrawable(-0000); popuWindow.setBackgroundDrawable(cd); popuWindow.showAsDropDown(v);
注意这里设置背景并不会覆盖xml文件定义的背景。
4、 当有popuWindow.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。
5、 // 这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。当然这里很明显只能在Touchable下才能使用。
popuWindow.setOutsideTouchable(true);
android PopupWindow学习总结



