`
zhiweiofli
  • 浏览: 514027 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

关于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

 

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
分享到:
评论

相关推荐

    PopupWindow嵌套Demo

    下面我们将详细讨论如何处理这个异常,以及如何正确地进行PopupWindow的嵌套。 首先,"Unable to add window"这个异常通常是因为没有正确的Context导致的。在创建PopupWindow时,我们通常需要传入一个Context对象,...

    popupwindow以及渐变背景

    接下来,我们讨论如何为PopupWindow添加渐变背景。渐变背景可以通过自定义drawable资源实现,常用的渐变类型有线性渐变(Linear Gradient)和径向渐变(Radial Gradient)。这里以线性渐变为例: 1. 在res/drawable...

    android:PopupWindow的使用

    接下来,我们讨论如何显示`PopupWindow`。`PopupWindow`有两种基本的显示方式:一种是相对于某个`View`显示,另一种是相对于屏幕坐标显示。对于第一种方式,我们可以使用`showAsDropDown()`方法;对于第二种方式,...

    PopupWindow和ListView配合使用

    接下来,我们讨论如何在`PopupWindow`中嵌入一个`ListView`。首先,我们需要创建一个`ListView`布局文件,定义其样式和各个条目的布局。然后,在`Activity`或`Fragment`中实例化`ListView`,并为其设置适配器,填充...

    popupwindow列子解决黑边框,点击外部不消失

    下面我们将详细讨论如何解决这些问题。 首先,黑边框的出现通常是由于PopupWindow的背景设置不当导致的。Android系统默认为PopupWindow提供了一个黑色的背景,如果未自定义背景,可能会看到明显的黑边。解决这个...

    PopupWindow 例子 只适应大小 位置

    PopupWindow在Android开发中是一个非常实用的组件,它允许开发者创建弹出式窗口,可以用于显示额外的信息...这个例子就是关于如何创建一个只适应大小并定位的PopupWindow,帮助你在Android开发中实现更灵活的界面交互。

    android弹出PopupWindow

    以上就是关于“android弹出PopupWindow”的详细讲解,包括如何自定义`PopupWindow`、解决显示位置问题、实现点击按钮后的弹出效果,以及dp和px单位的转换。通过理解并实践这些知识,你将能够更加灵活地运用`...

    实现半透明的popupwindow的源码

    接下来,我们讨论如何实现半透明效果。这主要涉及到设置PopupWindow的背景颜色和透明度。在Android中,颜色可以通过ARGB(Alpha、Red、Green、Blue)格式来表示,其中Alpha通道控制透明度。一个完全不透明的颜色值是...

    android popupwindow 单选、复选对话框

    本篇将详细讲解如何使用PopupWindow来创建单选和复选对话框,并讨论如何适应不同的适配器。 一、PopupWindow基础 PopupWindow是Android提供的一个可以自定义布局的类,它可以在屏幕上的任意位置弹出。创建...

    PopupWindow遮罩层

    在描述中提到的博客链接虽然无法直接访问,但根据常见的PopupWindow用法,我们可以讨论以下关键知识点: 1. **PopupWindow的基本使用**: - 创建PopupWindow实例时,需要传入一个View,这是PopupWindow的内容视图...

    PopupWindow

    在标签“PopupWindow”中,我们确认了主要讨论的对象,即PopupWindow的使用。 在压缩包文件“MyBottomMenuActivity”中,我们可以推测这是一个示例活动,展示如何在屏幕底部显示一个PopupWindow菜单。通常,这样的...

    popupwindow使用案例

    接下来,我们讨论如何设置PopupWindow的显示位置。PopupWindow的位置可以通过`showAtLocation()`或`showAsDropDown()`方法来控制。`showAtLocation()`方法需要传入一个View作为参考,以及相对于该View的x、y偏移量:...

    Android PopupWindow的menu和4.12版本拦截home键

    接下来,我们讨论一下“4.12版本的home拦截”。在Android中,Home键是一个特殊键,通常用于返回到主屏幕。默认情况下,应用程序无法拦截Home键,因为这是系统的保留行为。然而,从Android 3.1版本开始,引入了...

    圆角PopupWindow对话框和圆角EditText

    接下来,我们讨论圆角EditText。EditText是用户输入文本的基本组件,通常用于收集用户信息。在Android中,EditText的默认样式也是矩形。要将其转变为圆角,同样有两种方法:一种是设置带圆角的9-patch背景,另一种是...

    ListView和PopupWindow集合模仿的微信的

    接着,我们讨论PopupWindow。PopupWindow是Android中一个非常灵活的组件,它可以用来创建各种弹出式对话框,比如下拉菜单、提示框等。PopupWindow的使用包括设置布局、尺寸、背景、动画以及显示位置等步骤。在本项目...

    popupwindow实现底部弹框

    最后,关于提供的压缩包文件"PopupWindow弹底部对话框",里面可能包含了实现此功能的代码示例,包括布局文件、PopupWindow的创建和显示、以及按钮事件处理等相关代码。学习这个示例可以帮助开发者更好地理解和运用...

    PopupWindow的使用,按钮下弹出菜单

    在这个场景中,我们讨论的是如何利用`PopupWindow`来实现一个简单的按钮点击后下弹出菜单的功能。 首先,我们需要了解`PopupWindow`的基本概念。`PopupWindow` 类继承自 `ViewGroup`,它可以显示一个任意的 `View` ...

Global site tag (gtag.js) - Google Analytics