Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

  • AlertDialog的位置固定,而PopupWindow的位置可以随意
  • AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

  • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
  • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
  • showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

下面通过一个Demo讲解(解释看注释):

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <Button
  11. android:id="@+id/button01"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="以自己为Anchor,不偏移" />
  15. <Button
  16. android:id="@+id/button02"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="以自己为Anchor,有偏移" />
  20. <Button
  21. android:id="@+id/button03"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="以屏幕中心为参照,不偏移(正中间)" />
  25. <Button
  26. android:id="@+id/button04"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:text="以屏幕下方为参照,下方中间" />
  30. </LinearLayout>

popup_window.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:background="#00FF00"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="选择状态:"
  11. android:textColor="@android:color/white"
  12. android:textSize="20px" />
  13. <RadioGroup
  14. android:id="@+id/radioGroup"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical" >
  18. <RadioButton android:text="在线" />
  19. <RadioButton android:text="离线" />
  20. <RadioButton android:text="隐身" />
  21. </RadioGroup>
  22. </LinearLayout>

PopupWindowDemoActivity.java

  1. package com.tianjf;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.PopupWindow;
  10. import android.widget.RadioGroup;
  11. import android.widget.RadioGroup.OnCheckedChangeListener;
  12. public class PopupWindowDemoActivity extends Activity implements OnClickListener,
  13. OnCheckedChangeListener {
  14. private Button mbutton01;
  15. private Button mbutton02;
  16. private Button mbutton03;
  17. private Button mbutton04;
  18. private PopupWindow mPopupWindow;
  19. // 屏幕的width
  20. private int mScreenWidth;
  21. // 屏幕的height
  22. private int mScreenHeight;
  23. // PopupWindow的width
  24. private int mPopupWindowWidth;
  25. // PopupWindow的height
  26. private int mPopupWindowHeight;
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.main);
  31. mbutton01 = (Button) findViewById(R.id.button01);
  32. mbutton02 = (Button) findViewById(R.id.button02);
  33. mbutton03 = (Button) findViewById(R.id.button03);
  34. mbutton04 = (Button) findViewById(R.id.button04);
  35. mbutton01.setOnClickListener(this);
  36. mbutton02.setOnClickListener(this);
  37. mbutton03.setOnClickListener(this);
  38. mbutton04.setOnClickListener(this);
  39. }
  40. @Override
  41. public void onClick(View v) {
  42. switch (v.getId()) {
  43. // 相对某个控件的位置(正左下方),无偏移
  44. case R.id.button01:
  45. getPopupWindowInstance();
  46. mPopupWindow.showAsDropDown(v);
  47. break;
  48. // 相对某个控件的位置(正左下方),有偏移
  49. case R.id.button02:
  50. getPopupWindowInstance();
  51. mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50
  52. break;
  53. // 相对于父控件的位置,无偏移
  54. case R.id.button03:
  55. getPopupWindowInstance();
  56. mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
  57. break;
  58. // 相对于父控件的位置,有偏移
  59. case R.id.button04:
  60. getPopupWindowInstance();
  61. mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. @Override
  68. public void onCheckedChanged(RadioGroup group, int checkedId) {
  69. mPopupWindow.dismiss();
  70. }
  71. /*
  72. * 获取PopupWindow实例
  73. */
  74. private void getPopupWindowInstance() {
  75. if (null != mPopupWindow) {
  76. mPopupWindow.dismiss();
  77. return;
  78. } else {
  79. initPopuptWindow();
  80. }
  81. }
  82. /*
  83. * 创建PopupWindow
  84. */
  85. private void initPopuptWindow() {
  86. LayoutInflater layoutInflater = LayoutInflater.from(this);
  87. View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);
  88. RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);
  89. radioGroup.setOnCheckedChangeListener(this);
  90. // 创建一个PopupWindow
  91. // 参数1:contentView 指定PopupWindow的内容
  92. // 参数2:width 指定PopupWindow的width
  93. // 参数3:height 指定PopupWindow的height
  94. mPopupWindow = new PopupWindow(popupWindow, 100, 130);
  95. // 获取屏幕和PopupWindow的width和height
  96. mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
  97. mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();
  98. mPopupWindowWidth = mPopupWindow.getWidth();
  99. mPopupWindowHeight = mPopupWindow.getHeight();
  100. }
  101. }
 

Android中的PopupWindow详解的更多相关文章

  1. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  2. Android中mesure过程详解

    我们在编写layout的xml文件时会碰到layout_width和layout_height两个属性,对于这两个属性我们有三种选择:赋值成具体的数值,match_parent或者wrap_conte ...

  3. Android中Intent组件详解

    Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...

  4. Android中的动画详解系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自定义动画,这一篇我们来看看如何将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 如 ...

  5. Android中shape属性详解

    一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...

  6. RxJava在Android中使用场景详解

    RxJava 系列文章 <一,RxJava create操作符的用法和源码分析> <二,RxJava map操作符用法详解> <三,RxJava flatMap操作符用法 ...

  7. Android中的Service详解

    今天我们就来介绍一下Android中的四大组件中的服务Service,说到Service, 它分为本地服务和远程服务:区分这两种服务就是看客户端和服务端是否在同一个进程中,本地服务是在同一进程中的,远 ...

  8. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  9. Android中SQLite应用详解

    上次我向大家介绍了SQLite的基本信息和使用过程,相信朋友们对SQLite已经有所了解了,那今天呢,我就和大家分享一下在Android中如何使用SQLite. 现在的主流移动设备像Android.i ...

随机推荐

  1. JS的基础语法

    8.运算符号表达式 ①数学运算符 数学运算符有+.-.*./除().%(余数) var a = 10; var b = 5; alert(a+b); 预览以后在网页上弹出的对话框数值就是15. ②逻辑 ...

  2. GCD创建一个单例

    1.+(id)shareInstance{ static ClassA *A=nil; static dispatch_once_t onceToken; dispatch_once(&onc ...

  3. Map:比较新增加日期的和需要删除的日期 使用方法

    1.场景描述:根据在日历选择的日期,数据库来保持我们选择日期. 2.方法,硬删除的方法,每次全部删除,然后再重新添加选择的新的日期.这样导致如果需要保存create_time的情况,那么每次操作的都是 ...

  4. leetcode single number系列

    这个系列一共有三题,第一题是一组数里除了一个数出现一次之外,其他数都是成对出现,求这个数. 第二题是一组数里除了两个数出现一次外,其他数都是成对出现,求这两个数 第三题是一组数里除了一个数出现一次外, ...

  5. Swift 2.0 到底「新」在哪?

    [编者按]2015年6月,一年一度的苹果 WWDC 大会如期而至,在大会上苹果发布了 Swift 2.0,引入了很多新的特性,以帮助开发者更快.更简单地构建应用.本篇文章作者是 Maxime defa ...

  6. MVC 中 Razor 无限分类的展示

    在MVC的Razor视图展示无级分类的办法,在网上看了很多资料,大多搞得很高大上.可能本人水平有限,实在是不会用. 那我就用最简单爆力的办法来做. Model: public class NewsCa ...

  7. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  8. jquery ajax post 传递数组 ,多checkbox 取值

    jquery ajax post 传递数组 ,多checkbox 取值 http://w8700569.iteye.com/blog/1954396 使用$.each(function(){});可以 ...

  9. foreach的参数不是数组:Warning: Invalid argument supplied for foreach

    Warning: Invalid argument supplied for foreach() 问题Warning: Invalid argument supplied for foreach() ...

  10. WCF分布式开发步步为赢(10):请求应答(Request-Reply)、单向操作(One-Way)、回调操作(Call Back).

    WCF除了支持经典的请求应答(Request-Reply)模式外,还提供了什么操作调用模式,他们有什么不同以及我们如何在开发中使用这些操作调用模式.今天本节文章里会详细介绍.WCF分布式开发步步为赢( ...