popupWindow 用法总结 控制位置
Activity
}
Activity2
}
工具类
}
常用API
构造方法
- public PopupWindow(View contentView)
- public PopupWindow(Context context)
- public PopupWindow(View contentView, int width, int height)
- public PopupWindow(View contentView, int width, int height, boolean focusable)

- contentView为要显示的view;PopupWindow没有默认布局,它不会像AlertDialog那样只setTitle就能弹出来一个框,所以contentView必须设置。
- width和height为要显示的view的宽和高,值为像素值或MATCHT_PARENT、WRAP_CONTENT;如果View是从xml得到的,那么xml的第一层view的大小属性将被忽略;必须设置宽和高,否则显示不出来。
- focusable为是否能获得焦点
show方法
- showAsDropDown(View anchor) 相对某个控件的位置(正左下方),无偏移。最终调用的是下面的方法
- showAsDropDown(View anchor, int xoff, int yoff) 相对某个控件的位置,有偏移,xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;最终调用的是下面的方法
- showAsDropDown(View anchor, int xoff, int yoff, int gravity) 【注:这个方法貌似不起作用】相对某个控件的某个位置,有偏移。gravity的默认值为DEFAULT_ANCHORED_GRAVITY= Gravity.TOP | Gravity.START
- showAtLocation(View parent, int gravity, int x, int y) 相对于父控件(不管view设置的是哪一个,参照物都是整个屏幕的根布局)的某个位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移。

/*** Displays the content view in a popup window anchored to the corner of* another view. The window is positioned according to the specified* gravity and offset by the specified x and y coordinates.* <p>* 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* view can be scrolled, the specified vertical gravity will be ignored and* the popup will anchor itself such that it is visible.* <p>* If the view later scrolls to move <code>anchor</code> to a different* location, the popup will be moved correspondingly.** @param anchor the view on which to pin the popup window* @param xoff A horizontal offset from the anchor in pixels* @param yoff A vertical offset from the anchor in pixels* @param gravity Alignment of the popup relative to the anchor** @see #dismiss()*/public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)/*** <p>* Display the content view in a popup window at the specified location. If the popup window* cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams}* for more information on how gravity and the x and y parameters are related. Specifying* a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying* <code>Gravity.LEFT | Gravity.TOP</code>.* </p>** @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from* @param gravity the gravity which controls the placement of the popup window* @param x the popup's x location offset* @param y the popup's y location offset*/public void showAtLocation(View parent, int gravity, int x, int y)
setFocusable
/*** <p>Changes the focusability of the popup window. When focusable, the* window will grab the focus from the current focused widget if the popup* contains a focusable {@link android.view.View}. By default a popup* window is not focusable.</p>** <p>If the popup is showing, calling this method will take effect only* the next time the popup is shown or through a manual call to one of* the {@link #update()} methods.</p>** @param focusable true if the popup should grab focus, false otherwise.** @see #isFocusable()* @see #isShowing()* @see #update()*/public void setFocusable(boolean focusable) {mFocusable = focusable;}
setOutsideTouchable
/*** <p>Controls whether the pop-up will be informed of touch events outside* of its window. This only makes sense for pop-ups that are touchable* but not focusable, which means touches outside of the window will* be delivered to the window behind. The default is false.</p>** <p>If the popup is showing, calling this method will take effect only* the next time the popup is shown or through a manual call to one of* the {@link #update()} methods.</p>** @param touchable true if the popup should receive outside* touch events, false otherwise** @see #isOutsideTouchable()* @see #isShowing()* @see #update()*/public void setOutsideTouchable(boolean touchable) {mOutsideTouchable = touchable;}
附件列表
popupWindow 用法总结 控制位置的更多相关文章
- Android popupwindow 弹出的位置问题
在Android开发中,需要用到PopupWindow这个类.在初始化完成,显示之前,都需要获得这个对象的width,height去计算popupWindow弹出的位置. 这个时候会发现取得的widt ...
- Android中PopupWindow用法
参考资料链接:http://developer.android.com/reference/android/widget/PopupWindow.html 在Android中有很多级别的Window, ...
- Android PopupWindow怎么合理控制弹出位置(showAtLocation)
说到PopupWindow,应该都会有种熟悉的感觉,使用起来也很简单 // 一个自定义的布局,作为显示的内容 Context context = null; // 真实环境中要赋值 int layou ...
- 屏幕旋转时调用PopupWindow update方法更新位置失效的问题及解决方案
接到一个博友的反馈,在屏幕旋转时调用PopupWindow的update方法失效.使用场景如下:在一个Activity中监听屏幕旋转事件,在Activity主布局文件中有个按钮点击弹出一个Pop ...
- PopupWindow计算弹出位置
1.首先自定义PopupWindow popWindowView= LinearLayout.inflate(context, R.layout.popupWindow,null); po ...
- opencv播放视屏并控制位置
原文地址:http://blog.csdn.net/augusdi/article/details/9000592 cvGetCaptureProperty是我们需要使用到的获取视频属性的函数. do ...
- 一些特殊ACII码的用法 在控制台中覆盖显示、刷新显示和删除字符
很好奇怎么实现在控制台中不换行直接显示新的信息把旧的替换掉,于是找到了两个ACII码字符,他们可以帮助实现. 一个是‘\b’字符,这个字符是backspace,即删除上一个字符,于是可以清除以显示的旧 ...
- Unity 3D 中实现对物体 位置(position) 旋转(rotation) 大小(scale) 的全面控制
今天分享一些基础控制的脚本 1.位置(Position): 控制位置很简单,首先要知道要在xyz哪几个轴上移动,确定好后定义代表着那些轴的移动变量,速度(m_speed在函数外定义为全局变量)然后通过 ...
- android PopupWindow显示位置
PopupWindow的显示及位置设置 window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10); 第一个参数指定Pop ...
随机推荐
- Loadrunner中cookie解释与用法
loadrunner对于cookie的处理loadrunner中与cookie处理相关的常用函数如下: web_add_cookie():添加新的cookie或者修改已经存在的cookie web_r ...
- python的select和epoll
python的select和epoll 1.select模型: linux中每个套接字都是文件,都有唯一的文件描述符,这些设备的文件描述符被放在一个数组中,然后select调用的时候遍历这个数组,如果 ...
- EOJ 3260 袋鼠妈妈找孩子
暴力搜索. 主要目的就是找到任意一条路径,使得路径长度大于等于$k+1$,写个爆搜发现很快能出解.判断某点是否可走,需要看四周有没有已经走过的点的$dis$比这个点的$dis$小$2$或者$2$以上. ...
- cdoj1092-韩爷的梦 (字符串hash)【hash】
http://acm.uestc.edu.cn/#/problem/show/1092 韩爷的梦 Time Limit: 200/100MS (Java/Others) Memory Limi ...
- 分布式锁的理解,java自带的锁为什么会失效
前段时间在发送短信的代码块上通过网上找的工具类基于Redis实现了分布式锁的功能 对应的链接https://www.cnblogs.com/c-h-y/p/9391602.html 周末想细细看一下. ...
- arm Linux 驱动LED子系统 测试
Linux内核在3.0以上引入了设备树概念(具体哪个版本不清楚)在编译内核后需要将与之对应的dtb文件也下载人板子上才能使内核与硬件关联起来. dtb文件是有dts文件编译后生成的:例如 /* * C ...
- C#多线程编程实战(一):线程基础
1.1 简介 为了防止一个应用程序控制CPU而导致其他应用程序和操作系统本身永远被挂起这一可能情况,操作系统不得不使用某种方式将物理计算分割为一些虚拟的进程,并给予每个执行程序一定量的计算能力.此外操 ...
- Linux-数据库3
外键约束 如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表. 外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操 ...
- Chris and Magic Square CodeForces - 711B
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...
- HihoCoder - 1756 打怪
题面在这里! 拆成两个部分分别算显然比较简单. 前面一个部分排个序枚举最大值算就好啦. 后面的就相当于把每一种数值的贡献加起来,也可以在排完序之后的a[]上面直接算出来. #include<bi ...