关于PopupWindow的议论

关于PopupWindow的讨论

 

说到android的PopupWindow弹出窗,与对话框Dialog的区别就是,PopupWindow可以灵活定制弹出窗的界面以及弹出的位置!

 

对,控制弹出窗的弹出位置,这个是我在这里主要想讨论,分享的地方。

 

弹出前,先创建一个popupWindow的实例:

 

		private PopupWindow createPopupWindow(){
			LayoutInflater factory = LayoutInflater.from(ctx);  //加载popWindow的layout
			final View textEntryView = factory.inflate(R.layout.relative_layout_popup_win, null);
			textEntryView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {  //响应弹出窗被点击后的响应
					popWin.dismiss();
				}
			});
			return new PopupWindow(textEntryView,450,90); //请注意,使用此构造方法,一定要指定popupWindow的长宽,之后调用popupWindow的showAXXX方法才会显示指定的contentView
		}

 

这里有个地方要注意的,PopupWindow的构造方法有几个,当你想要在构造阶段就指定外貌时,请留意了,留心下面官方的说明,当不传递width,height参数时,构造出来的popupWindow的dimension只有0*0

 

也就是无论怎么调用showAXXX的方法,弹出来的窗体,你都无法见到啦,因为是0面积嘛。

 

public  PopupWindow   (View   contentView, int width, int height)

Since:  API Level 1

Create a new non focusable popup window which can display the  contentView . The dimension of the window must be passed to this constructor.

The popup does not provide any background. This should be handled by the content view.

Parameters
contentView width height
the popup's content
the popup's width
the popup's height

public  PopupWindow   (View   contentView)

Since:  API Level 1

Create a new non focusable popup window which can display the  contentView . The dimension of the window are (0,0).

The popup does not provide any background. This should be handled by the content view.

Parameters
contentView
the popup's content

 

当然你也可以再后面初始化popupWindow时,再调用其他功能函数进行设置。

 

 

创建完窗体后,就要设置其弹出的位置了:

 

先看看官方的说明:

 

void showAsDropDown (View   anchor, int xoff, int yoff)

Display the content view in a popup window anchored to the bottom-left corner of the anchor view offset by the specified x and y coordinates.

void showAsDropDown (View   anchor)

Display the content view in a popup window anchored to the bottom-left corner of the anchor view.

void showAtLocation (View   parent, int gravity, int x, int y)

Display the content view in a popup window at the specified location.

 

 

具体就两种方式:showAsDropDown 和 showAtLocation ,

 

showAsDropDown

  •  就是以触发弹出窗弹出的那个view为基准 弹出 后显示在那个view的正下方,view的下方,并是水平*!

  •  假如有偏移的参数,则显示的位置按参数进行x,y坐标的偏移, 参数xoff yoff,默认为0,传入负数向左偏移,正数向右偏移

 

showAtLocation   

  •  自定义弹出的位置,参数列里有parent,与 showAsDropDown anchor不同性质,注释里是这样说的: a parent view to get the  getWindowToken()   token from,也就是与弹出的位置没有直接关联;

  •  参数gravity,大致与TextView的gravity一样,指示控件的对齐方向,当然这里的基准对象当然是DisplayWindow啦;

  •  参数 x,y 就是指示弹出窗实际的显示坐标了,但这两个参数依赖参数gravity,指示基准对齐后的xy偏移值,这点与showAsDropDown 的一样

 

 

 

下面是我的例子,实现弹出在触发控件的正上方位置。

 

int  offsetY =  -parentView.getHeight()-popWin.getHeight();
popWin.showAsDropDown(parentView, 0, offsetY);

 

不过这里的showAsDropDown 要注一个比较有趣的地方,下面是官方的注释:

 

Display the content view in a popup window anchored to the bottom-left corner of the anchor view offset by the specified x and y coordinates.

If there is not enough room on screen to show the popup in its entirety, this method tries to find a parent scroll view to scroll.

If no parent scroll view can be scrolled, the bottom-left corner of the popup is pinned at the top left corner of the anchor view.

If the view later scrolls to move  anchor   to a different location, the popup will be moved correspondingly.

 

注意那个 If 的句子,它们大致意思就是,你必须确保触发弹出窗所在的view要有足够的空间去让弹出窗弹出显示,否则弹出显示的位置将由popupWindow自己去适应!

所以要设计好触发弹出动作的view的布局,尽量让其父容器要有足够的控件去容纳弹出的popupWindow,否则popupWindow弹出的位置将会不定。

这种情况在android的多种设备的屏幕上显示,更是要注意啊!

 

 

其实popupWindow还有update的方法,去更改其显示的位置的,这里就不一一讨论了,反正原理一样。

假如文章里有什么纰漏,希望各位看官多多指教,多多交流。谢谢。