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 ...
随机推荐
- bzoj 3000 Big Number 估算n!在k进制下的位数 斯特林公式
题目大意 求n!在k进制下的位数 2≤N≤2^31, 2≤K≤200 分析 作为数学没学好的傻嗨,我们先回顾一下log函数 \(\log_a(b)=\frac 1 {log_b(a)}\) \(\lo ...
- net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件
原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] fileToXml类: using System;using System.Data;using System.C ...
- Javascript 开启浏览器全屏模式
作者:伯乐在线/前端空城师 通常在某些情况下,我们需要让浏览器开启全屏模式,以便获得更好的视觉体验,先看下全屏模式简单的几个API. 浏览器默认绑定 非全屏模式下, document的F11按键绑定开 ...
- 转 使用putty从linux主机上面往windows主机下面拷贝文件
更新一下,把putty的包解压以后,想要在dos窗口中直接使用,必须把putty解压的文件的路径添加到环境变量中,这样使用起来就会非常简单了. 郁闷了好久,终于搞定了putty的上传下载文件命令psc ...
- wpf LookUpEdit PopupContentTemplate
<dxg:LookUpEdit Name="searchLookUpEdit" HorizontalAlignment="Stretch" PopupHe ...
- PHP获取今天开始和结束的时间戳
$t = time();$start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$ ...
- [Inside HotSpot] UseParallelGC和UseParallelOldGC的区别
JVM的很多参数命名很有迷惑性,-XX:+UseParallel,-XX:+UseParallelOldGC,-XX:+UseParNewGC,-XX:+UseConcMarkSweepGC咋一看容易 ...
- 网络流24T 太空飞行计划问题
题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的 ...
- [BZOJ5286][洛谷P4425][HNOI2018]转盘(线段树)
5286: [Hnoi2018]转盘 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 15 Solved: 11[Submit][Status][Di ...
- 2017 [六省联考] T1 期末考试
4868: [Shoi2017]期末考试 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 842 Solved: 385[Submit][Status ...