关于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
contentViewthe popup's content
widththe popup's width
heightthe 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
contentViewthe 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

 

showAtLocation   

 

 

 

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

 

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的方法,去更改其显示的位置的,这里就不一一讨论了,反正原理一样。

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值