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的使用场景和注意事项的更多相关文章

  1. Android PopupWindow Dialog 关于 is your activity running 崩溃详解

    Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...

  2. Android PopupWindow的使用和分析

    Android PopupWindow的使用和分析 PopupWindow使用 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activi ...

  3. 关于文章“cocos2dx移植android平台-我的血泪史”需要注意事项

    关于文章"cocos2dx移植android平台-我的血泪史"需要注意事项 在上次转载的这篇文章中,按照配置一步一步的下去.发现工程中在Android.mk中有一处错误.直接bui ...

  4. Android PopupWindow的使用技巧(转)

    Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...

  5. JSON的使用场景及注意事项介绍

    上篇我们讲解了JSON的诞生原因是因为XML整合到HTML中各个浏览器实现的细节不尽相同,所以道格拉斯·克罗克福特(Douglas Crockford) 和 奇普·莫宁斯达(Chip Mornings ...

  6. 大约Android PopupWindow有用Spinner控件点击APP Crash案例整理!

    场景异常,如下面: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.V ...

  7. Android PopupWindow使用方法小结

    前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法. 使用场景 PopupWindow,顾名思义,就是弹窗,在很多 ...

  8. 客户端相关知识学习(一)之混合开发,为什么要在App中使用H5页面以及应用场景、注意事项

    混合开发 随着移动互联网的高速发展,常规的开发速度已经渐渐不能满足市场需求.原生H5混合开发应运而生,目前,市场上许多主流应用都有用到混合开发,例如支付宝.美团等.下面,结合我本人的开发经验,简单谈一 ...

  9. Android popupwindow使用心得(一)

    最近项目中好多地方用到popupwindow,感觉这个控件还是非常重要的.所以把使用心得总结下,废话不多说,直接上代码. public class MainActivity extends Activ ...

随机推荐

  1. Linux内核解析之标准I/O库

    当Linux创建一个进程时,会自动创建3个文件描述符0,1,2,分别对应标准输入,标准输出,错误输出.C库中与文件描述符对应的是文件指针.查看C库头文件stdio.h中的源码 typedef stru ...

  2. cpu乱序执行

    http://blog.163.com/zhaojie_ding/blog/static/1729728952007925111324379/?suggestedreading 处理器的乱序和并发执行 ...

  3. 解决win2008下IIS7的HTTP500错误

    造成500错误常见原因有:ASP语法出错.ACCESS数据库连接语句出错.文件引用与包含路径出错.使用了服务器不支持的组件如FSO等.另外,对于win2008的IIS默认不显示详细出错信息的问题以下就 ...

  4. AC日记——dispatching bzoj 2809

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3290  Solved: 1740[Submi ...

  5. va_list 简介

    原文:http://blog.sina.com.cn/s/blog_590be5290100qhxr.html va_list是一个宏,由va_start和va_end界定. typedef char ...

  6. django删除表重建&修改用户密码&base64加密解密字符串&ps aux参数说明&各种Error例子

    1.django的queryset不支持负索引 AssertionError: Negative indexing is not supported. 2.django向前端JavaScript传递列 ...

  7. git grep 或者 ag 进行快速代码搜索

    1.git grep foo 会自动map所有包含foo的文件 2.git grep -n foo  显示行号 3.git grep --name-only foo 只显示文件名 4.git grep ...

  8. Jenkins自动化部署入门(一)

    开始使用 Jenkins 这一段时间,技术总监为了减少测试环境每次提交新增接口都要部署项目的时间,搞了一个jenkins持续集成github.docker,这样只要每次push代码都会自动部署,确实节 ...

  9. hadoop之hdfs------------------FileSystem及其源码分析

    FileSystem及其源码分析 FileSystem这个抽象类提供了丰富的方法用于对文件系统的操作,包括上传.下载.删除.创建等.这里多说的文件系统通常指的是HDFS(DistributedFile ...

  10. The Process class relies on proc_open, which is not available on your PHP installation

    [Symfony\Component\Process\Exception\RuntimeException] The Process class relies on proc_open, which ...