Android中的PopupWindow详解
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
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/button01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以自己为Anchor,不偏移" />
- <Button
- android:id="@+id/button02"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以自己为Anchor,有偏移" />
- <Button
- android:id="@+id/button03"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以屏幕中心为参照,不偏移(正中间)" />
- <Button
- android:id="@+id/button04"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以屏幕下方为参照,下方中间" />
- </LinearLayout>
popup_window.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FF00"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="选择状态:"
- android:textColor="@android:color/white"
- android:textSize="20px" />
- <RadioGroup
- android:id="@+id/radioGroup"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <RadioButton android:text="在线" />
- <RadioButton android:text="离线" />
- <RadioButton android:text="隐身" />
- </RadioGroup>
- </LinearLayout>
PopupWindowDemoActivity.java
- package com.tianjf;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.PopupWindow;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class PopupWindowDemoActivity extends Activity implements OnClickListener,
- OnCheckedChangeListener {
- private Button mbutton01;
- private Button mbutton02;
- private Button mbutton03;
- private Button mbutton04;
- private PopupWindow mPopupWindow;
- // 屏幕的width
- private int mScreenWidth;
- // 屏幕的height
- private int mScreenHeight;
- // PopupWindow的width
- private int mPopupWindowWidth;
- // PopupWindow的height
- private int mPopupWindowHeight;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mbutton01 = (Button) findViewById(R.id.button01);
- mbutton02 = (Button) findViewById(R.id.button02);
- mbutton03 = (Button) findViewById(R.id.button03);
- mbutton04 = (Button) findViewById(R.id.button04);
- mbutton01.setOnClickListener(this);
- mbutton02.setOnClickListener(this);
- mbutton03.setOnClickListener(this);
- mbutton04.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- // 相对某个控件的位置(正左下方),无偏移
- case R.id.button01:
- getPopupWindowInstance();
- mPopupWindow.showAsDropDown(v);
- break;
- // 相对某个控件的位置(正左下方),有偏移
- case R.id.button02:
- getPopupWindowInstance();
- mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50
- break;
- // 相对于父控件的位置,无偏移
- case R.id.button03:
- getPopupWindowInstance();
- mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
- break;
- // 相对于父控件的位置,有偏移
- case R.id.button04:
- getPopupWindowInstance();
- mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);
- break;
- default:
- break;
- }
- }
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- mPopupWindow.dismiss();
- }
- /*
- * 获取PopupWindow实例
- */
- private void getPopupWindowInstance() {
- if (null != mPopupWindow) {
- mPopupWindow.dismiss();
- return;
- } else {
- initPopuptWindow();
- }
- }
- /*
- * 创建PopupWindow
- */
- private void initPopuptWindow() {
- LayoutInflater layoutInflater = LayoutInflater.from(this);
- View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);
- RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);
- radioGroup.setOnCheckedChangeListener(this);
- // 创建一个PopupWindow
- // 参数1:contentView 指定PopupWindow的内容
- // 参数2:width 指定PopupWindow的width
- // 参数3:height 指定PopupWindow的height
- mPopupWindow = new PopupWindow(popupWindow, 100, 130);
- // 获取屏幕和PopupWindow的width和height
- mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
- mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();
- mPopupWindowWidth = mPopupWindow.getWidth();
- mPopupWindowHeight = mPopupWindow.getHeight();
- }
- }
Android中的PopupWindow详解的更多相关文章
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- Android中mesure过程详解
我们在编写layout的xml文件时会碰到layout_width和layout_height两个属性,对于这两个属性我们有三种选择:赋值成具体的数值,match_parent或者wrap_conte ...
- Android中Intent组件详解
Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...
- Android中的动画详解系列【4】——Activity之间切换动画
前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自定义动画,这一篇我们来看看如何将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 如 ...
- Android中shape属性详解
一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...
- RxJava在Android中使用场景详解
RxJava 系列文章 <一,RxJava create操作符的用法和源码分析> <二,RxJava map操作符用法详解> <三,RxJava flatMap操作符用法 ...
- Android中的Service详解
今天我们就来介绍一下Android中的四大组件中的服务Service,说到Service, 它分为本地服务和远程服务:区分这两种服务就是看客户端和服务端是否在同一个进程中,本地服务是在同一进程中的,远 ...
- Android中Service 使用详解(LocalService + RemoteService)
Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- Android中SQLite应用详解
上次我向大家介绍了SQLite的基本信息和使用过程,相信朋友们对SQLite已经有所了解了,那今天呢,我就和大家分享一下在Android中如何使用SQLite. 现在的主流移动设备像Android.i ...
随机推荐
- jetty启动报错Unsupported major.minor version 51.0
主要是JDK版本的问题,需要将Eclipse的Jdk版本设置为1.7的才可以,编译级别也设置为1.7,然后删除maven项目路径,D:\WORK\workspace\xxx\target下的所有文件, ...
- [nowCoder] 二进制中1的个数
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. class Solution { public: int NumberOf1(int n) { ; while(n) ...
- java输出流实现文件下载
//导出Excel try { HSSFWorkbook wb = carService.export(list); //调用service方法~! response.setContentType(& ...
- Sqli-labs less 27
Less-27 本关主要考察将union,select和26关过滤掉的字符.此处我们依旧和26关的方式是一样的,只需要将union和select改为大小写混合就可以突破. 示例:127.0.0.1/s ...
- 单片机模拟 1/2 Bias、1/4 Duty的 LCD 驱动使用方法
工作原理 方式一 根据 LCD 的驱动原理可知,LCD 像素点上只能加上 AC 电压,LCD 显示器的对比度由 COM脚上的电压值减去 SEG 脚上的电压值决定,当这个电压差大于 LCD 的饱 ...
- Ado.Net要知道的东西
什么是ADO.NET? ADO.NET就是一组类库,这组类库可以让我们通过程序的方式访问数据库,就像System.IO下的类用类操作文件一样,System.Data.这组类是用来操作数据库(不光是MS ...
- C# 设置程序开机自动运行(+注册表项)
有时候我们需要让软件安装好了,开机自动运行,这时我们需要把启动项加载到注册表中,需要注意的时现在很多杀毒软件在其他软件更改注册表的时候会有提示,可能会阻止.下面代码包含增加启动项到注册表和删除启动项. ...
- 《JavaScript基础教程(第8版)》PDF
简介:JavaScript基础教程(第8版)循序渐进地讲述了JavaScript及相关的CSS.DOM.Ajax.jQuery等技术.书中从JavaScript语言基础开始,分别讨论了图像.框架.浏览 ...
- poj 2762(强连通+判断链)
题目链接:http://poj.org/problem?id=2762 思路:首先当然是要缩点建新图,由于题目要求是从u->v或从v->u连通,显然是要求单连通了,也就是要求一条长链了,最 ...
- Shell脚本基础I
1.Linux shell类型 /bin/sh--已经被/bin/bash所取代 /bin/bash--就是Linux预设的shell /bin/ksh--由AT&T Bell lab发展出来 ...