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. Java-河内塔问题

    河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edouard Lucas曾 ...

  2. 百度图表echars插件使用案例

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  3. Scanner用法

    先来看一个简单的例子: import java.util.*; public class ScannerTest { public static void main(String[] args){   ...

  4. PostgreSQL 二进制安装

    一.安装前准备工作 新建用户 sudo groupadd sql sudo useradd -g sql postgressudo passwd postgres 创建数据及日志目录,并做相应授权 s ...

  5. ZSTU 4241 圣杯战争(ST表+二分)

    题目链接  ZSTU 4241 问题转化为有很多区间,现在每次给定一个区间求这个区间和之前所有区间中的某一个的交集的最大长度. 强制在线. 首先我们把所有的区间预处理出来. 然后去重(那些被包含的小区 ...

  6. facebook面试题【转】

    1. 给两个类A和Bclass A {public void foo (A a) { ...}}class B extends A {public void foo (B b) { ...}}问这么写 ...

  7. HAXM 6.0.5显示不兼容Windows

    HAXM 6.0.5显示不兼容Windows 最近更新Android后,用户会在Android Manager中发现,以前可以安装Intel x86模拟器现在不能安装了.提示错误信息如下:intel  ...

  8. SSM整合案例

    使用IDEA整合SSM spring核心配置文件:beans_core.xml/applicationContext.xml <?xml version="1.0" enco ...

  9. Linux 在VMware中搭建CentOS6.5虚拟机

    原文:http://www.cnblogs.com/PurpleDream/p/4263465.html Linux 在VMware中搭建CentOS6.5虚拟机 前言:      本文主要是我在大家 ...

  10. excel怎么把文本格式的数字转换为数字,且把前面的撇号去掉

    excel把文本格式的数字转换为数字,且把前面的撇号去掉方法:1.选中要处理的列,在“数据”菜单下,选择“分列”.2.在“分列”向导对话框里,选择“分隔符号”,并点击下一步.3.在“分列”向导对话框第 ...