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 ...
随机推荐
- UVA10298 Power Strings [KMP]
题目传送门 Power Strings 格式难调,题面就不放了. 一句话题意,求给定的若干字符串的最短循环节循环次数. 输入样例#1: abcd aaaa ababab . 输出样例#1: 1 4 3 ...
- Git in Powershell saying 'Could not find ssh-agent'
加入系统环境变量D:\Program Files\Git\usr\bin
- dp 动规 最佳加法表达式
最佳加法表达式 有一个由1..9组成的数字串.问如果将m个加号插入到这个数字串中,在各种可能形成的表达式中,值最小的那个表达式的值是多少 解题思路 假定数字串长度是n,添完加号后,表达式的最后一个加号 ...
- Redis学习篇(六)之ZSet类型及其操作
ZADD 作用: 将元素及其分数添加到集合中 语法: ZADD key score membre [score member] 当集合元素已经存在时,再次添加会更新其分数 当score是 +inf 时 ...
- [TJOI2017]DNA --- 后缀数组
[TJOI2017]DNA 题目描述 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S, 有这个序列的碱基序列就会表现出喜欢吃藕的性状,但是研究人员发现对碱基序列S,任意修改其中不超过3个 ...
- MySQL注射绕过技巧(三)
在测试一次注入的时候发现过滤了逗号 所以找到这个思路 第一次遇到的时候是看key哥挖洞 遇到后就想记录下来 正文 过滤了逗号 利用join来逐步查询 select*from(select 1)a ...
- java中代码块的认知
在以往面试题中,出现过很多次关于代码块执行顺序的题目,目前可算是搞清楚执行顺序了.看以下代码: package com.itcast.code; public class BlockTest { st ...
- SPFA cojs 176. [USACO Feb07] 奶牛聚会
cojs 176. [USACO Feb07] 奶牛聚会 ★☆ 输入文件:sparty.in 输出文件:sparty.out 简单对比时间限制:3 s 内存限制:16 MB N(1 ≤ ...
- PHP -- 模拟测试上传文件
FROM :http://web.itivy.com/article-740-1.html QQ上传文件为什么那么快? “QQ上传大文件/QQ群发送大文件时,可以在极短的时间内完成”是如何做到的. 有 ...
- 使用OData快速构建REST服务
OData是微软支持的一种查询标准,它的第四版使用了REST规范,看起来简洁多了.它的最大的特点是可以在客户端自行配制查询条件,使用它构建REST服务时再也不用担心查询的扩展性问题了. 如下是几个简单 ...