---
title: "Android开发之PopupWindow"
author: "itoracja"
date: "2012-01-31T13:48:42Z"
tags: []
slug: "android-popupwindow"
draft: false
lastmod: "2012-01-31T13:48:42Z"
id: 1386664
url: "https://itoracja.iteye.com/blog/1386664"
---

<h1>Android开发之PopupWindow</h1>
<p>/*</p>
<p>* Android开发之PopupWindow</p>
<p>*</p>
<p>* Created on: 2011-8-8</p>
<p>* Author: blueeagle</p>
<p>* Email: liujiaxiang@gmail.com</p>
<p>*/</p>
<p> 聪明的人善于总结，记录，不知道这是谁说的了，反正要当一个聪明人，我得先学会总结，记录。最近在Android的学习过程中，发现PopupWindow也是值得研究一番的一个东东，因此拿过来说道说道。与其相似的就归纳到一起说道吧，那就是AlertDialog和Toast。</p>
<h2>PopupWindow</h2>
<p>java.lang.Object </p>
<p> ? android.widget.PopupWindow</p>
<p>手册中，对PopupWindow的描述是这样的：它是一个弹出的窗口，可以用来显示一个任意的视图。弹出窗口是当前活动上出现一个浮动容器。</p>
<p> PopupWindow的性质，其实就是我们通常所说的“模态对话框”。只有在其退出之后，才可以进行外部线程的操作。下面对其进行详细说明。</p>
<h3>简单的PopupWindow</h3>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883088dqj5.gif"></p>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883108uFWu.gif"></p>
<p> 如图所示，通过主Activity中的一个按钮来触发弹出窗口操作。这个窗口什么都不干，就是显示一句话：“狂热的Android开发者”。为了增加可见性，我设置了不同的背景色加以区分。</p>
<p> 源码如下：</p>
<textarea readonly name="code" class="java">/*

 *  Android开发之PopupWindow、AlertDialog和Toast

 *  PopWinEx.java

 *  Created on: 2011-8-8

 *  Author: blueeagle

 *  Email: liujiaxiang@gmail.com

 */

package com.blueeagle;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.PopupWindow;

 

public class PopWinEx extends Activity {

    /** Called when the activity is first created. */

    Button MyButton;

    PopupWindow pw;

    View myView;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        MyButton = (Button)findViewById(R.id.myButton);

        

        MyButton.setOnClickListener(new Button.OnClickListener(){

 

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub            

              myView = getLayoutInflater().inflate(R.layout.pop,null);  

              pw = new PopupWindow(myView,500,200,true); 

              pw.showAsDropDown(MyButton);

           }

        

        });

    }

}

</textarea><p><br></p>
<p>我们给PopupWindow和主界面，分别定义一个layout。对应的XML文件如下：</p>
<textarea readonly name="code" class="html">pop.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ffff00"

    &gt;

&lt;TextView  

    android:id="@+id/mytextview"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    /&gt;

&lt;/LinearLayout&gt;

main.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ff00ff"

    &gt;

&lt;Button  

    android:id="@+id/myButton"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    /&gt;

&lt;/LinearLayout&gt;

</textarea><p><br></p>
<p><strong>说明：这里值得注意的是，我们要给弹出窗口设置焦点，</strong>pw = <strong>new</strong> PopupWindow(myView,500,200,<strong>true</strong>);<strong>这句中的true表示弹出窗口可以获得焦点。如果弹出窗口没有获得焦点的时候，不断点击按钮的，最终程序将退出，这时是因为内存耗尽的原因，可以通过查看log得到。</strong></p>
<p><strong></strong></p>
<h3>有一定布局的PopupWindow</h3>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883128M11K.gif"></p>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883208azyM.gif"></p>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883226t3zX.gif"></p>
<p>如图所示，同样是通过主Activity中的一个按钮来触发弹出窗口操作。但是现在这个弹出窗口可以进行一些操作，因为其具有了一定的布局，目前我们暂且其可以进行的操作为：</p>
<p>1. 可编辑功能</p>
<p>2. 可传递信息</p>
<p>3. 弹出窗口上再次弹出窗口</p>
<p>4. 可以取消当前弹出窗口</p>
<p>为了增加可见性，我依然设置了不同的背景色加以区分。</p>
<p> 源码如下：</p>
<textarea readonly name="code" class="java">/*

 *  Android开发之PopupWindow、AlertDialog和Toast

 *  PopWinEx.java

 *  Created on: 2011-8-9

 *  Author: blueeagle

 *  Email: liujiaxiang@gmail.com

 */

 

package com.blueeagle;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

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.EditText;

import android.widget.PopupWindow;

import android.widget.TextView;

 

public class PopWinEx extends Activity {

    /** Called when the activity is first created. */

    Button MyButton;

    Button MyExit;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        MyButton = (Button)findViewById(R.id.myButton);

        MyExit = (Button)findViewById(R.id.myExit);

       MyButton.setOnClickListener(new clickEvent());

       MyExit.setOnClickListener(new ExitEvent());

       

 

    }

    class clickEvent implements OnClickListener {

 

       @Override

       public void onClick(View v) {

           // TODO Auto-generated method stub

           if(v==MyButton) {

              showPopWindow(PopWinEx.this,MyButton);

           }

           }

       }

    class ExitEvent implements OnClickListener {

 

       @Override

       public void onClick(View v) {

           // TODO Auto-generated method stub

           if(v==MyExit) {

              finish();

           }

           }

       }

    private void showPopWindow(Context context, View parent) {

       // TODO Auto-generated method stub

       final PopupWindow pw;       

       View myView;      

       LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       myView = inflater.inflate(R.layout.pop, null);      

       Button pop_OK = (Button)myView.findViewById(R.id.button_ok);

       Button pop_Cancel = (Button)myView.findViewById(R.id.button_cancel);

       final EditText pop_User = (EditText)myView.findViewById(R.id.edittext);

       final EditText pop_Password = (EditText)myView.findViewById(R.id.password);     

       pw = new PopupWindow(myView,500,200,true);       

       pop_OK.setOnClickListener(new OnClickListener(){

           @Override

           public void onClick(View v) {

              showPop_PopWindow();

              // TODO Auto-generated method stub                   

           }

           private void showPop_PopWindow() {

              // TODO Auto-generated method stub

              View myView1;

              myView1 = getLayoutInflater().inflate(R.layout.pop1,null);

              TextView User_Is = (TextView)myView1.findViewById(R.id.useris);

              TextView Password_Is = (TextView)myView1.findViewById(R.id.passwordis);

              User_Is.setText(pop_User.getText().toString());

              Password_Is.setText(pop_Password.getText().toString());

              final PopupWindow pw_pw;

              pw_pw = new PopupWindow(myView1,500,200,true);

              

              Button pop_pop_OK = (Button)myView1.findViewById(R.id.button_ok1);

              Button pop_pop_Cancel = (Button)myView1.findViewById(R.id.button_cancel1);

              pop_pop_Cancel.setOnClickListener(new OnClickListener(){

                  @Override

                  public void onClick(View v) {

                     pw_pw.dismiss();//       

                     

                  }

               });            

              pop_pop_OK.setOnClickListener(new OnClickListener(){

                  @Override

                  public void onClick(View v) {

                      AlertDialog.Builder my_ADialog =new AlertDialog.Builder(PopWinEx.this);

                      my_ADialog.setTitle("我是弹出对话框的弹出对话框");

                      my_ADialog.setMessage("怎么样？学会了吗？");

                      my_ADialog.show();     

                     

                  }

               });

              pw_pw.showAtLocation(myView1.findViewById(R.id.button_ok1), Gravity.CENTER, 200, 200);

           }

        });     

       pop_Cancel.setOnClickListener(new OnClickListener(){

           @Override

           public void onClick(View v) {

              pw.dismiss();//                  

           }

        });  

       pw.showAsDropDown(MyButton);

    }   

}

</textarea><p><br></p>
<p>我们给PopupWindow，弹出窗口的弹出窗口和主界面，分别定义一个layout。对应的XML文件如下：</p>
<textarea readonly name="code" class="html">main.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ff00ff"

    &gt;

&lt;Button  

    android:id="@+id/myButton"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    /&gt;

    &lt;Button  

    android:id="@+id/myExit"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="退出程序"

    /&gt;

&lt;/LinearLayout&gt;

pop1.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ffff00"

    &gt;

&lt;TextView  

    android:id="@+id/mytextview"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    /&gt;

    &lt;TextView  

    android:text="您输入的账号是："

    android:layout_width="180dip" 

    android:layout_height="wrap_content" 

    /&gt;

    &lt;TextView  

    android:id="@+id/useris"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    /&gt;

    &lt;TextView  

    android:text="您输入的密码是："

    android:layout_width="180dip" 

    android:layout_height="wrap_content" 

    /&gt;

    &lt;TextView  

    android:id="@+id/passwordis"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    /&gt;

    &lt;LinearLayout 

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ffff00"

    &gt;

    &lt;Button  

    android:id="@+id/button_ok1"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="确定"

    /&gt;

    &lt;Button  

    android:id="@+id/button_cancel1"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="取消"

    /&gt;

    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;

pop.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ffff00"

    &gt;

&lt;TextView  

    android:id="@+id/mytextview"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    /&gt;

    &lt;TextView  

    android:id="@+id/mytextview"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="账号"

    /&gt;

&lt;EditText  

    android:id="@+id/edittext"

    android:layout_width="180dip" 

    android:layout_height="wrap_content" 

    /&gt;

    &lt;TextView  

    

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="密码"

    /&gt;

    &lt;EditText  

    android:id="@+id/password"

    android:layout_width="180dip" 

    android:layout_height="wrap_content" 

    android:password="true"

    /&gt;

    &lt;LinearLayout 

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ffff00"

    &gt;

    &lt;Button  

    android:id="@+id/button_ok"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="确定"

    /&gt;

    &lt;Button  

    android:id="@+id/button_cancel"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="取消"

    /&gt;

    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;

</textarea><p align="left"><br></p>
<p align="left"><strong>说明：这里值得注意的是，弹出窗口上再次弹出窗口需要将调用</strong>findViewById<strong>函数的视图明确。然后将相应的变量设置成final类型。弹出窗口其实是一个View，这个View需要用</strong> LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.<em>LAYOUT_INFLATER_SERVICE</em>);</p>
<p> myView = inflater.inflate(R.layout.<em>pop</em>, <strong>null</strong>);<strong>来进行索引。找到相应的xml布局文件，来安排弹出窗口是什么样的。当然，在实际开发中，可能会遇见没有xml布局文件的View，这怎么办呢？直接new出来就可以了，比如：</strong></p>
<p align="left">  mView = <strong>new</strong> PopView(mContext, 800, 400);</p>
<p>  PopupWindow pw= <strong>new</strong> PopupWindow(mView,800,400,<strong>true</strong>);</p>
<p><strong>这里我没有对string做特别的处理，因为时间比较紧张。这并不是一个好的编程习惯。希望大家把语言类的东西都放在string里去，不要硬写在程序代码里。</strong></p>
<h3>PopupWindow的特殊效果</h3>
<p><img alt="" src="http://hi.csdn.net/attachment/201108/9/0_1312883254K44X.gif"></p>
<p>PopupWindow的效果可以做的很炫，可以有边框，圆角，透明，渐变色，动画等。下面逐一来实现。</p>
<p>比如我要在上面的例子中实现这些操作，即可添加一个语句：</p>
<p> myView.setBackgroundResource(R.drawable.<em>round_win</em>);</p>
<p>当然，最主要的就是round_win.xml里面所写的内容了：</p>
<textarea readonly name="code" class="html">round_win.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;    

&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"

        android:shape="rectangle"&gt;

        &lt;gradient android:startColor="#e0000fff" android:endColor="#e000ff00"

                android:angle="90" /&gt;&lt;!--背景颜色渐变 --&gt;

        &lt;stroke android:dashWidth="2dp" android:dashGap="2dp"

                android:width="2dp" android:color="#ff0000"&gt;&lt;/stroke&gt;

        &lt;!--描边 --&gt;

        &lt;corners android:bottomRightRadius="5dp"

                android:bottomLeftRadius="5dp" android:topLeftRadius="5dp"

                android:topRightRadius="5dp" /&gt;&lt;!--设置圆角--&gt;

&lt;/shape&gt;

</textarea><p><br></p>
<p>当然，最主要的就是round_win.xml里面所写的内容了。对于上面，这条 shape 的定义，分别为渐变，在gradient 中startColor属性为开始的颜色，endColor 为渐变结束的颜色，下面的 angle 是角度。接下来是 stroke可以理解为边缘，dashWidth 表示宽度，dashGap 表示断续；corners 为拐角这里radius 属性为半径，最后是相对位置属性 padding。</p>
<p>android:color 是一个16 进制颜色。这个颜色由RGB 值指定，可带Alpha 。必须以“# ”开头，后面跟随Alpha-Red-Green-Blue。Alpha表示的是透明度，0为全透明，ff为全不透明。</p>
<p> 以上完成了弹出窗口的边框，渐变色，透明，圆角等操作。</p>
<p>下面进行有意思的动画操作。</p>
<p> 先设置一下弹出窗口的进入和退出的xml文件。首先要在res目录下建立文件夹anim，里面建立动画描述文件。如下所示：</p>
<textarea readonly name="code" class="html">ani_in.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;set xmlns:android="http://schemas.android.com/apk/res/android"&gt;

    &lt;translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"

       android:fromYDelta="-100"

       android:toYDelta="0"

       android:duration="1000"

       android:fillEnabled="true"

       android:fillAfter="true"

       /&gt;

        &lt;scale android:fromXScale="0.6" android:toXScale="1.0"

                android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"

                android:pivotY="50%" android:duration="2000" /&gt;

        &lt;alpha android:interpolator="@android:anim/decelerate_interpolator"

                android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" /&gt;

&lt;/set&gt;

ani_out.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;set xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="true"

    &gt;

    &lt;translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"

       android:fromYDelta="0"

       android:toYDelta="-100"

       android:duration="1000"

       android:fillEnabled="true"

       android:fillAfter="true"

       /&gt;

               &lt;scale android:fromXScale="1.0" android:toXScale="0.4"

                android:fromYScale="1.0" android:toYScale="0.4" android:pivotX="50%"

                android:pivotY="50%" android:duration="2000" /&gt;

    &lt;alpha android:interpolator="@android:anim/decelerate_interpolator"

       android:fromAlpha="1.0" 

       android:toAlpha="0.0" 

       android:duration="2000" 

       /&gt;

&lt;/set&gt;

</textarea><p><br></p>
<p>在Values文件夹中建立style.xml文件如下：</p>
<p><strong><em>style.xml</em></strong></p>
<p></p>
<textarea readonly name="code" class="html">&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;resources&gt;

    &lt;style name="mytheme" parent="@android:style/Theme.Light"&gt;

       &lt;item name="android:windowNoTitle"&gt; true &lt;/item&gt;

    &lt;/style&gt;

 

 

    &lt;style name="PopupAnimation" parent="android:Animation"&gt;

       &lt;item name="android:windowEnterAnimation"&gt;@anim/ani_in&lt;/item&gt;

       &lt;item name="android:windowExitAnimation"&gt;@anim/ani_out&lt;/item&gt;

    &lt;/style&gt;

&lt;/resources&gt;

</textarea><p><br></p>
<p> 最后在Activity代码文件中加入：</p>
<p> pw.setAnimationStyle(R.style.<em>PopupAnimation</em>);</p>
<p>动画效果就出来了，怎么样？很炫吧。</p>