android:PopupWindow的使用场景和注意事项
1.PopupWindow的特点
借用Google官方的说法:
“A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.”
也就是说。popupwindow是activity上方的一个悬浮容器。它能够显示随意的视图View,非常霸气的样子。
以下看一下,它怎样使用的。
2.初始化PopupWindow的一些特性
举例:
PopupWindow popupWindow = new PopupWindow(getApplicationContext());
popupWindow.setContentView(contentView);//能够设置随意的View
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);//设置宽度
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);//高度
popupWindow.setAnimationStyle(R.anim.abc_fade_in);//显示的动画
popupWindow.setFocusable(true);//设置是否获取焦点
当中,contentView是你想要显示的View。这个view,你能够使用LayoutInflater.from(context).inflate映射对应的xml文件
3.PopupWindow的显示和隐藏
显示的方法:
public void showAtLocation (View parent, int gravity, int x, int y)
Added in API level 1
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 WindowManager.LayoutParams for more information on how gravity and the x and y parameters are related. Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP. Parameters
parent a parent view to get the getWindowToken() token from
gravity the gravity which controls the placement of the popup window
x the popup's x location offset
y the popup's y location offset
popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, 0);//设置居中
popupWindow.showAtLocation(contentView, Gravity.NO_GRAVITY, x, y);//显示窗体的以(x,y)为左上角的位置
隐藏:
if (popupWindow != null
&& popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
相关:注意,在计算view的位置时:
Android里面提供了一些方法能够获取View在屏幕中的位置。
1).getLocationOnScreen ,计算该视图在全局坐标系中的x,y值,获取在当前屏幕内的绝对坐标(该值从屏幕顶端算起。包含了通知栏高度)。
2).getLocationInWindow ,计算该视图在它所在的widnow的坐标x,y值。
3)getLeft , getTop, getBottom, getRight, 这一组是获取相对在它父亲布局里的坐标。
相关:popupwindow动画:http://blog.csdn.net/wl455624651/article/details/7798879
很多其它交流,Android开发联盟QQ群:272209595
android:PopupWindow的使用场景和注意事项的更多相关文章
- Android PopupWindow Dialog 关于 is your activity running 崩溃详解
Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...
- Android PopupWindow的使用和分析
Android PopupWindow的使用和分析 PopupWindow使用 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activi ...
- 关于文章“cocos2dx移植android平台-我的血泪史”需要注意事项
关于文章"cocos2dx移植android平台-我的血泪史"需要注意事项 在上次转载的这篇文章中,按照配置一步一步的下去.发现工程中在Android.mk中有一处错误.直接bui ...
- Android PopupWindow的使用技巧(转)
Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...
- JSON的使用场景及注意事项介绍
上篇我们讲解了JSON的诞生原因是因为XML整合到HTML中各个浏览器实现的细节不尽相同,所以道格拉斯·克罗克福特(Douglas Crockford) 和 奇普·莫宁斯达(Chip Mornings ...
- 大约Android PopupWindow有用Spinner控件点击APP Crash案例整理!
场景异常,如下面: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.V ...
- Android PopupWindow使用方法小结
前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法. 使用场景 PopupWindow,顾名思义,就是弹窗,在很多 ...
- 客户端相关知识学习(一)之混合开发,为什么要在App中使用H5页面以及应用场景、注意事项
混合开发 随着移动互联网的高速发展,常规的开发速度已经渐渐不能满足市场需求.原生H5混合开发应运而生,目前,市场上许多主流应用都有用到混合开发,例如支付宝.美团等.下面,结合我本人的开发经验,简单谈一 ...
- Android popupwindow使用心得(一)
最近项目中好多地方用到popupwindow,感觉这个控件还是非常重要的.所以把使用心得总结下,废话不多说,直接上代码. public class MainActivity extends Activ ...
随机推荐
- C#.net制作验证码(英文与数字组成的4位随机数),以及MD5值的使用
原文发布时间为:2008-09-22 -- 来源于本人的百度文章 [由搬家工具导入] 参考资料:http://www.cnblogs.com/gwazy/articles/139510.html 三个 ...
- 标准C程序设计七---73
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 使用C语言和i2c-dev驱动
原文地址:blog.csdn.NET/wyt2013/article/details/20740659 感谢作者分享. 在本博客的<使用Beaglebone Black的I2C(一)>中, ...
- Perl语言入门--4--列表
1.列表也是数组的形式:(1,'a',2,3,4) 元素可以是任意类型,变量,表达式 2.空列表:() 单元素列表:(2) .与值2不同 qw(1 $a str) #qw是用空格作为分隔符,元素 ...
- Laravel 5.1 框架返回状态拦截
Laravel 5.1 返回信息状态拦截 可以在app\Exceptions\Handler.php文件中修改, public function render($request, Exception ...
- IOS 改变Navigation的返回按钮
两个办法: 1, 手动为每一个UIViewController添加navigationItem的leftButton的设置代码 2,为UINavigationController实现delegate, ...
- cmake ccmake
下载libqrencode源码编译过程 git clone https://github.com/fukuchi/libqrencode.git 2001 mkdir build 2002 cd ...
- jenkins+gitlab发布maven项目
1.简介:什么是Maven Maven是一个项目管理和综合工具.Maven提供给开发人员构建一个完整的生命周期框架; 开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生 ...
- hdu6223(后缀数组)
题意: 给一个长度为n的字符串s[0..n-1],但i的后继不再是i+1,而是(i*i+1)%n,求所有长度为n的“子串”中,字典序最大的是谁 n<=150000 分析: 如果是一般的字符串,那 ...
- eclipse默认配色(内含恢复文件和恢复方法)
转载:http://blog.csdn.net/w174504744/article/details/8672679 很多搞开发的同学一开始不喜欢默认的eclipse白底配色,去网上千辛万苦搜到了很多 ...