PopupWindow学习总结

来源:互联网 发布:海牛多多运营软件 编辑:程序博客网 时间:2024/05/19 17:51

经过一段时间的学习,了解了PopupWindow的基本用法,如下:

1、为PopupWindow的view布局,通过LayoutInflator获取布局的view.如:

LayoutInflaterinflater=(LayoutInflater)this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

ViewtextEntryView=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=newPopupWindow(anchor.getContext());

this.window.setTouchInterceptor(newOnTouchListener(){

@Override

publicbooleanonTouch(Viewv,MotionEventevent){

if(event.getAction()==MotionEvent.ACTION_OUTSIDE){

BetterPopupWindow.this.window.dismiss();

returntrue;

}

returnfalse;

}

});

教训:

1、PopuWindow的大小由下面代码控制;

newPopupWindow(view,ViewGroup.LayoutParams.FILL_PARENT,

ViewGroup.LayoutParams.WRAP_CONTENT);

2、popuWindow.showAsDropDown(v);方法是将PopuWindow显示在Viewv的左下方;

3、需要顺利让PopUpWindowdimiss(即点击PopuWindow之外的地方此或者back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在popuWindow.showAsDropDown(v);或者其它的显示PopuWindow方法之前设置它的背景不为空:如下面两行代码:

ColorDrawablecd=newColorDrawable(-0000);

popuWindow.setBackgroundDrawable(cd);

popuWindow.showAsDropDown(v);

注意这里设置背景并不会覆盖xml文件定义的背景。

4、当有popuWindow.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。

5、//这里设置显示PopuWi

原创粉丝点击