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 ...
随机推荐
- Windows 命令行切换目录
Windows 命令行切换目录 特别注意:切换到其它盘符不需要 cd 命令 1. 切换到 C 盘根目录 打开终端 cmd 后,输入cd C:\(一定要加上后面的反斜扛) 2.切换到 C 盘子目录 打开 ...
- 将内存图像数据封装成QImage V2
转:http://www.cnblogs.com/bibei1234/p/3161555.html 如何将内存图像数据封装成QImage 当采用Qt开发相机数据采集软件时,势必会遇到采集内存图像并进行 ...
- 2017-2018-1 20179202《Linux内核原理与分析》第五周作业
一.系统调用实验: 1.知识准备: (1)系统调用的三层皮:xyz()(API).system_call(所有系统调用的入口) . sys_xyz()(中断服务程序) (2)API是程序员在用户空间下 ...
- centos7-硬盘坏道检测
#检测坏道命令,结果输出到 /home/badblocks.log badblock -s -v -o /home/badblocks.log /dev/sdb1 [注]:硬盘损坏程度不同,block ...
- 【HDU 6020】 MG loves apple (乱搞?)
MG loves apple Accepts: 20 Submissions: 693 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: ...
- android 网络
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha android - async - http 安卓 异步 超文本传输协议 xUtil a ...
- ElasticSearch学习笔记--2、ES相关配置
1.配置文件 ES的配置文件位置:config/elasticsearch.yml可以直接搜索elasticsearch.yml 2.配置远程api访问 network.host: 192.168.1 ...
- [转]android 自定义圆形imageview控件
android布局 首先,定义定义圆形Imageview类: import android.content.Context; import android.graphics.Bitmap; imp ...
- Codeforces Beta Round #4 (Div. 2 Only) A. Watermelon 水题
A. Watermelon 题目连接: http://www.codeforces.com/contest/4/problem/A Description One hot summer day Pet ...
- 05-树6. Path in a Heap (25) 小根堆
05-树6. Path in a Heap (25) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...