关于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
    评论
旅游社交小程序功能有管理员和用户。管理员有个人中心,用户管理,每日签到管理,景点推荐管理,景点分类管理,防疫查询管理,美食推荐管理,酒店推荐管理,周边推荐管理,分享圈管理,我的收藏管理,系统管理。用户可以在微信小程序上注册登录,进行每日签到,防疫查询,可以在分享圈里面进行分享自己想要分享的内容,查看和收藏景点以及美食的推荐等操作。因而具有一定的实用性。 本站后台采用Java的SSM框架进行后台管理开发,可以在浏览器上登录进行后台数据方面的管理,MySQL作为本地数据库,微信小程序用到了微信开发者工具,充分保证系统的稳定性。系统具有界面清晰、操作简单,功能齐全的特点,使得旅游社交小程序管理工作系统化、规范化。 管理员可以管理用户信息,可以对用户信息添加修改删除。管理员可以对景点推荐信息进行添加修改删除操作。管理员可以对分享圈信息进行添加,修改,删除操作。管理员可以对美食推荐信息进行添加,修改,删除操作。管理员可以对酒店推荐信息进行添加,修改,删除操作。管理员可以对周边推荐信息进行添加,修改,删除操作。 小程序用户是需要注册才可以进行登录的,登录后在首页可以查看相关信息,并且下面导航可以点击到其他功能模块。在小程序里点击我的,会出现关于我的界面,在这里可以修改个人信息,以及可以点击其他功能模块。用户想要把一些信息分享到分享圈的时候,可以点击新增,然后输入自己想要分享的信息就可以进行分享圈的操作。用户可以在景点推荐里面进行收藏和评论等操作。用户可以在美食推荐模块搜索和查看美食推荐的相关信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值