---
title: "Android之PopupWindow弹出对话框\r\n        Android之PopupWindow弹出对话框"
author: "mmdev"
date: "2013-01-20T23:01:00Z"
tags: []
slug: "android-popupwindow-android-popupwindow"
draft: false
lastmod: "2013-01-29T10:47:53Z"
id: 1777509
url: "https://mmdev.iteye.com/blog/1777509"
---

### [Android之PopupWindow弹出对话框](http://blog.csdn.net/lcfeng1982/article/details/6906856)

分类：[Android](http://blog.csdn.net/lcfeng1982/article/category/814071)2011-10-26 13:47931人阅读[评论](http://blog.csdn.net/lcfeng1982/article/details/6906856#comments)(0)[收藏]()[举报](http://blog.csdn.net/lcfeng1982/article/details/6906856#report "举报")

Android的对话框常用的有两种：PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单，不同于AlertDialog对话框，PopupWindow弹出的**位置可以很多变化**，按照有无偏移分，可以分为**无偏移和偏移**两种；按照参照类型不同又可以分为两种：**相对某个控件（Anchor锚）的位置和父容器内部的相对位置**。具体如下：

函数 简介 showAsDropDown([View](http://blog.csdn.net/lcfeng1982/article/details/6906856)anchor) 相对某个控件的位置（正左下方），无偏移 showAsDropDown([View](http://blog.csdn.net/lcfeng1982/article/details/6906856)anchor, int xoff, int yoff) 相对某个控件的位置，有偏移 showAtLocation([View](http://blog.csdn.net/lcfeng1982/article/details/6906856)parent, int gravity, int x, int y) 父容器容器相对位置，例如正中央Gravity.CENTER，下方Gravity.BOTTOM等

下面是运行程序截图：

![](http://hi.csdn.net/attachment/201104/30/0_1304151337hAW2.gif)![](http://hi.csdn.net/attachment/201104/30/0_1304151342z44u.gif)

![](http://hi.csdn.net/attachment/201104/30/0_1304151350n9K6.gif)![](http://hi.csdn.net/attachment/201104/30/0_1304151354oq0L.gif)

程序代码：

布局：main.xml

[view plain](http://blog.csdn.net/kkfdsa132/article/details/6376518 "view plain")

01. &lt;?xmlversion="1.0"encoding="utf-8"?&gt;
02. &lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03. android:id="@+id/layout"
04. android:orientation="vertical"
05. android:layout\_width="fill\_parent"
06. android:layout\_height="fill\_parent"
07. &gt;
08. &lt;TextView
09. android:id="@+id/tv\_showText"
10. android:layout\_width="fill\_parent"
11. android:layout\_height="wrap\_content"
12. android:gravity="center"
13. android:text="@string/hello"
14. android:textSize="22px"
15. /&gt;
16. &lt;Button
17. android:id="@+id/bt\_PopupWindow1"
18. android:text="以自己为Anchor，不偏移"
19. android:layout\_width="fill\_parent"
20. android:layout\_height="wrap\_content"
21. /&gt;
22. &lt;Button
23. android:id="@+id/bt\_PopupWindow2"
24. android:text="以自己为Anchor，正下方"
25. android:layout\_width="fill\_parent"
26. android:layout\_height="wrap\_content"
27. /&gt;
28. &lt;Button
29. android:id="@+id/bt\_PopupWindow3"
30. android:text="以屏幕中心为参照，不偏移（正中间）"
31. android:layout\_width="fill\_parent"
32. android:layout\_height="wrap\_content"
33. /&gt;
34. &lt;Button
35. android:id="@+id/bt\_PopupWindow4"
36. android:text="以屏幕下方为参照，下方中间"
37. android:layout\_width="fill\_parent"
38. android:layout\_height="wrap\_content"
39. /&gt;
40. &lt;/LinearLayout&gt;

自定义对话框dialog.xml

[view plain](http://blog.csdn.net/kkfdsa132/article/details/6376518 "view plain")

01. &lt;?xmlversion="1.0"encoding="utf-8"?&gt;
02. &lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout\_width="fill\_parent"
05. android:layout\_height="fill\_parent"
06. &gt;
07. &lt;TextView
08. android:id="@+id/tv\_tip"
09. android:layout\_width="fill\_parent"
10. android:layout\_height="wrap\_content"
11. android:text="请输入内容："
12. /&gt;
13. &lt;EditText
14. android:id="@+id/et\_text"
15. android:layout\_width="fill\_parent"
16. android:layout\_height="wrap\_content"
17. &gt;&lt;/EditText&gt;
18. &lt;LinearLayout
19. android:gravity="center\_horizontal"
20. android:layout\_width="fill\_parent"
21. android:layout\_height="fill\_parent"
22. &gt;
23. &lt;Button
24. android:id="@+id/bt\_ok"
25. android:text="确定"
26. android:layout\_width="100px"
27. android:layout\_height="50px"
28. /&gt;
29. &lt;Button
30. android:id="@+id/bt\_cancle"
31. android:text="取消"
32. android:layout\_width="100px"
33. android:layout\_height="50px"
34. /&gt;
35. &lt;/LinearLayout&gt;
36. &lt;/LinearLayout&gt;

代码：

[view plain](http://blog.csdn.net/kkfdsa132/article/details/6376518 "view plain")

001. packagecom.myandroid.test;
002. importandroid.app.Activity;
003. importandroid.content.Context;
004. importandroid.content.SharedPreferences.Editor;
005. importandroid.os.Bundle;
006. importandroid.util.Log;
007. importandroid.view.Gravity;
008. importandroid.view.LayoutInflater;
009. importandroid.view.View;
010. importandroid.view.View.OnClickListener;
011. importandroid.view.ViewGroup.LayoutParams;
012. importandroid.widget.Button;
013. importandroid.widget.EditText;
014. importandroid.widget.Gallery;
015. importandroid.widget.PopupWindow;
016. importandroid.widget.TextView;
017. publicclassPopupWindowTestextendsActivity{//PopupWindow属于不阻塞的对话框，AlertDialog则是阻塞的。
018. privateButtonbt\_popupWindow1;
019. privateButtonbt\_popupWindow2;
020. privateButtonbt\_popupWindow3;
021. privateButtonbt\_popupWindow4;
022. privateTextViewtv\_showText;
023. privatePopupWindowpopupWindow;
024. privateintscreenWidth;
025. privateintscreenHeight;
026. privateintdialgoWidth;
027. privateintdialgoheight;
028. /\*\*Calledwhentheactivityisfirstcreated.\*/
029. @Override
030. publicvoidonCreate(BundlesavedInstanceState){
031. super.onCreate(savedInstanceState);
032. setContentView(R.layout.main);
033. initView();
034. }
035. /\**
036. \*初始化控件和响应事件
037. \*/
038. privatevoidinitView(){
039. bt\_popupWindow1=(Button)findViewById(R.id.bt\_PopupWindow1);
040. bt\_popupWindow2=(Button)findViewById(R.id.bt\_PopupWindow2);
041. bt\_popupWindow3=(Button)findViewById(R.id.bt\_PopupWindow3);
042. bt\_popupWindow4=(Button)findViewById(R.id.bt\_PopupWindow4);
043. tv\_showText=(TextView)findViewById(R.id.tv\_showText);
044. bt\_popupWindow1.setOnClickListener(newClickEvent());
045. bt\_popupWindow2.setOnClickListener(newClickEvent());
046. bt\_popupWindow3.setOnClickListener(newClickEvent());
047. bt\_popupWindow4.setOnClickListener(newClickEvent());
048. }
049. /\**
050. \*按钮点击事件处理
051. \*@authorKobi
052. \*
053. \*/
054. privateclassClickEventimplementsOnClickListener{
055. @Override
056. publicvoidonClick(Viewv){
057. //TODOAuto-generatedmethodstub
058. switch(v.getId()){
059. caseR.id.bt\_PopupWindow1://以自己为Anchor，不偏移
060. getPopupWindow();
061. popupWindow.showAsDropDown(v);
062. break;
063. caseR.id.bt\_PopupWindow2://以自己为Anchor，偏移(screenWidth-dialgoWidth)/2,0)--按钮正下方
064. getPopupWindow();
065. popupWindow.showAsDropDown(v,(screenWidth-dialgoWidth)/2,0);
066. break;
067. caseR.id.bt\_PopupWindow3://以屏幕中心为参照，不偏移
068. getPopupWindow();
069. popupWindow.showAtLocation(findViewById(R.id.layout),Gravity.CENTER,0,0);
070. break;
071. caseR.id.bt\_PopupWindow4://以屏幕左下角为参照，偏移(screenWidth-dialgoWidth)/2,0)--屏幕下方中央
072. getPopupWindow();
073. popupWindow.showAtLocation(findViewById(R.id.layout),
074. Gravity.BOTTOM,0,0);
075. break;
076. default:
077. break;
078. }
079. }
080. }
081. /\**
082. \*创建PopupWindow
083. \*/
084. protectedvoidinitPopuptWindow(){
085. //TODOAuto-generatedmethodstub
086. ViewpopupWindow\_view=getLayoutInflater().inflate(//获取自定义布局文件dialog.xml的视图
087. R.layout.dialog,null,false);
088. popupWindow=newPopupWindow(popupWindow\_view,200,150,true);//创建PopupWindow实例
089. Buttonbt\_ok=(Button)popupWindow\_view.findViewById(R.id.bt\_ok);//dialog.xml视图里面的控件
090. Buttonbt\_cancle=(Button)popupWindow\_view.findViewById(R.id.bt\_cancle);
091. finalEditTextet\_text=(EditText)popupWindow\_view.findViewById(R.id.et\_text);
092. bt\_ok.setOnClickListener(newOnClickListener(){
093. @Override
094. publicvoidonClick(Viewv){
095. //TODOAuto-generatedmethodstub
096. tv\_showText.setText(et\_text.getText());//在标签里显示内容
097. popupWindow.dismiss();//对话框消失
098. }
099. });
100. bt\_cancle.setOnClickListener(newOnClickListener(){
101. @Override
102. publicvoidonClick(Viewv){
103. //TODOAuto-generatedmethodstub
104. popupWindow.dismiss();
105. }
106. });
107. //获取屏幕和对话框各自高宽
108. screenWidth=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
109. screenHeight=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();
110. dialgoWidth=popupWindow.getWidth();
111. dialgoheight=popupWindow.getHeight();
112. }
113. /*
114. \*获取PopupWindow实例
115. \*/
116. privatevoidgetPopupWindow(){
117. if(null!=popupWindow){
118. popupWindow.dismiss();
119. return;
120. }else{
121. initPopuptWindow();
122. }
123. }
124. @Override
125. protectedvoidonPause(){
126. //TODOAuto-generatedmethodstub
127. super.onPause();
128. Log.e("ActivityState","onPause");
129. }
130. @Override
131. protectedvoidonResume(){
132. //TODOAuto-generatedmethodstub
133. super.onResume();
134. Log.e("ActivityState","onResume");
135. }
136. }

Android的对话框常用的有两种：PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单，不同于AlertDialog对话框，PopupWindow弹出的**位置可以很多变化**，按照有无偏移分，可以分为**无偏移和偏移**两种；按照参照类型不同又可以分为两种：**相对某个控件（Anchor锚）的位置和父容器内部的相对位置**。具体如下：

函数 简介 showAsDropDown([View](http://blog.csdn.net/lcfeng1982/article/details/6906856)anchor) 相对某个控件的位置（正左下方），无偏移 showAsDropDown([View](http://blog.csdn.net/lcfeng1982/article/details/6906856)anchor, int xoff, int yoff) 相对某个控件的位置，有偏移 showAtLocation([View](http://blog.csdn.net/lcfeng1982/article/details/6906856)parent, int gravity, int x, int y) 父容器容器相对位置，例如正中央Gravity.CENTER，下方Gravity.BOTTOM等

下面是运行程序截图：

![](http://hi.csdn.net/attachment/201104/30/0_1304151337hAW2.gif)![](http://hi.csdn.net/attachment/201104/30/0_1304151342z44u.gif)

![](http://hi.csdn.net/attachment/201104/30/0_1304151350n9K6.gif)![](http://hi.csdn.net/attachment/201104/30/0_1304151354oq0L.gif)

程序代码：

布局：main.xml

[view plain](http://blog.csdn.net/kkfdsa132/article/details/6376518 "view plain")

01. &lt;?xmlversion="1.0"encoding="utf-8"?&gt;
02. &lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03. android:id="@+id/layout"
04. android:orientation="vertical"
05. android:layout\_width="fill\_parent"
06. android:layout\_height="fill\_parent"
07. &gt;
08. &lt;TextView
09. android:id="@+id/tv\_showText"
10. android:layout\_width="fill\_parent"
11. android:layout\_height="wrap\_content"
12. android:gravity="center"
13. android:text="@string/hello"
14. android:textSize="22px"
15. /&gt;
16. &lt;Button
17. android:id="@+id/bt\_PopupWindow1"
18. android:text="以自己为Anchor，不偏移"
19. android:layout\_width="fill\_parent"
20. android:layout\_height="wrap\_content"
21. /&gt;
22. &lt;Button
23. android:id="@+id/bt\_PopupWindow2"
24. android:text="以自己为Anchor，正下方"
25. android:layout\_width="fill\_parent"
26. android:layout\_height="wrap\_content"
27. /&gt;
28. &lt;Button
29. android:id="@+id/bt\_PopupWindow3"
30. android:text="以屏幕中心为参照，不偏移（正中间）"
31. android:layout\_width="fill\_parent"
32. android:layout\_height="wrap\_content"
33. /&gt;
34. &lt;Button
35. android:id="@+id/bt\_PopupWindow4"
36. android:text="以屏幕下方为参照，下方中间"
37. android:layout\_width="fill\_parent"
38. android:layout\_height="wrap\_content"
39. /&gt;
40. &lt;/LinearLayout&gt;

自定义对话框dialog.xml

[view plain](http://blog.csdn.net/kkfdsa132/article/details/6376518 "view plain")

01. &lt;?xmlversion="1.0"encoding="utf-8"?&gt;
02. &lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout\_width="fill\_parent"
05. android:layout\_height="fill\_parent"
06. &gt;
07. &lt;TextView
08. android:id="@+id/tv\_tip"
09. android:layout\_width="fill\_parent"
10. android:layout\_height="wrap\_content"
11. android:text="请输入内容："
12. /&gt;
13. &lt;EditText
14. android:id="@+id/et\_text"
15. android:layout\_width="fill\_parent"
16. android:layout\_height="wrap\_content"
17. &gt;&lt;/EditText&gt;
18. &lt;LinearLayout
19. android:gravity="center\_horizontal"
20. android:layout\_width="fill\_parent"
21. android:layout\_height="fill\_parent"
22. &gt;
23. &lt;Button
24. android:id="@+id/bt\_ok"
25. android:text="确定"
26. android:layout\_width="100px"
27. android:layout\_height="50px"
28. /&gt;
29. &lt;Button
30. android:id="@+id/bt\_cancle"
31. android:text="取消"
32. android:layout\_width="100px"
33. android:layout\_height="50px"
34. /&gt;
35. &lt;/LinearLayout&gt;
36. &lt;/LinearLayout&gt;

代码：

[view plain](http://blog.csdn.net/kkfdsa132/article/details/6376518 "view plain")

001. packagecom.myandroid.test;
002. importandroid.app.Activity;
003. importandroid.content.Context;
004. importandroid.content.SharedPreferences.Editor;
005. importandroid.os.Bundle;
006. importandroid.util.Log;
007. importandroid.view.Gravity;
008. importandroid.view.LayoutInflater;
009. importandroid.view.View;
010. importandroid.view.View.OnClickListener;
011. importandroid.view.ViewGroup.LayoutParams;
012. importandroid.widget.Button;
013. importandroid.widget.EditText;
014. importandroid.widget.Gallery;
015. importandroid.widget.PopupWindow;
016. importandroid.widget.TextView;
017. publicclassPopupWindowTestextendsActivity{//PopupWindow属于不阻塞的对话框，AlertDialog则是阻塞的。
018. privateButtonbt\_popupWindow1;
019. privateButtonbt\_popupWindow2;
020. privateButtonbt\_popupWindow3;
021. privateButtonbt\_popupWindow4;
022. privateTextViewtv\_showText;
023. privatePopupWindowpopupWindow;
024. privateintscreenWidth;
025. privateintscreenHeight;
026. privateintdialgoWidth;
027. privateintdialgoheight;
028. /\*\*Calledwhentheactivityisfirstcreated.\*/
029. @Override
030. publicvoidonCreate(BundlesavedInstanceState){
031. super.onCreate(savedInstanceState);
032. setContentView(R.layout.main);
033. initView();
034. }
035. /\**
036. \*初始化控件和响应事件
037. \*/
038. privatevoidinitView(){
039. bt\_popupWindow1=(Button)findViewById(R.id.bt\_PopupWindow1);
040. bt\_popupWindow2=(Button)findViewById(R.id.bt\_PopupWindow2);
041. bt\_popupWindow3=(Button)findViewById(R.id.bt\_PopupWindow3);
042. bt\_popupWindow4=(Button)findViewById(R.id.bt\_PopupWindow4);
043. tv\_showText=(TextView)findViewById(R.id.tv\_showText);
044. bt\_popupWindow1.setOnClickListener(newClickEvent());
045. bt\_popupWindow2.setOnClickListener(newClickEvent());
046. bt\_popupWindow3.setOnClickListener(newClickEvent());
047. bt\_popupWindow4.setOnClickListener(newClickEvent());
048. }
049. /\**
050. \*按钮点击事件处理
051. \*@authorKobi
052. \*
053. \*/
054. privateclassClickEventimplementsOnClickListener{
055. @Override
056. publicvoidonClick(Viewv){
057. //TODOAuto-generatedmethodstub
058. switch(v.getId()){
059. caseR.id.bt\_PopupWindow1://以自己为Anchor，不偏移
060. getPopupWindow();
061. popupWindow.showAsDropDown(v);
062. break;
063. caseR.id.bt\_PopupWindow2://以自己为Anchor，偏移(screenWidth-dialgoWidth)/2,0)--按钮正下方
064. getPopupWindow();
065. popupWindow.showAsDropDown(v,(screenWidth-dialgoWidth)/2,0);
066. break;
067. caseR.id.bt\_PopupWindow3://以屏幕中心为参照，不偏移
068. getPopupWindow();
069. popupWindow.showAtLocation(findViewById(R.id.layout),Gravity.CENTER,0,0);
070. break;
071. caseR.id.bt\_PopupWindow4://以屏幕左下角为参照，偏移(screenWidth-dialgoWidth)/2,0)--屏幕下方中央
072. getPopupWindow();
073. popupWindow.showAtLocation(findViewById(R.id.layout),
074. Gravity.BOTTOM,0,0);
075. break;
076. default:
077. break;
078. }
079. }
080. }
081. /\**
082. \*创建PopupWindow
083. \*/
084. protectedvoidinitPopuptWindow(){
085. //TODOAuto-generatedmethodstub
086. ViewpopupWindow\_view=getLayoutInflater().inflate(//获取自定义布局文件dialog.xml的视图
087. R.layout.dialog,null,false);
088. popupWindow=newPopupWindow(popupWindow\_view,200,150,true);//创建PopupWindow实例
089. Buttonbt\_ok=(Button)popupWindow\_view.findViewById(R.id.bt\_ok);//dialog.xml视图里面的控件
090. Buttonbt\_cancle=(Button)popupWindow\_view.findViewById(R.id.bt\_cancle);
091. finalEditTextet\_text=(EditText)popupWindow\_view.findViewById(R.id.et\_text);
092. bt\_ok.setOnClickListener(newOnClickListener(){
093. @Override
094. publicvoidonClick(Viewv){
095. //TODOAuto-generatedmethodstub
096. tv\_showText.setText(et\_text.getText());//在标签里显示内容
097. popupWindow.dismiss();//对话框消失
098. }
099. });
100. bt\_cancle.setOnClickListener(newOnClickListener(){
101. @Override
102. publicvoidonClick(Viewv){
103. //TODOAuto-generatedmethodstub
104. popupWindow.dismiss();
105. }
106. });
107. //获取屏幕和对话框各自高宽
108. screenWidth=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
109. screenHeight=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();
110. dialgoWidth=popupWindow.getWidth();
111. dialgoheight=popupWindow.getHeight();
112. }
113. /*
114. \*获取PopupWindow实例
115. \*/
116. privatevoidgetPopupWindow(){
117. if(null!=popupWindow){
118. popupWindow.dismiss();
119. return;
120. }else{
121. initPopuptWindow();
122. }
123. }
124. @Override
125. protectedvoidonPause(){
126. //TODOAuto-generatedmethodstub
127. super.onPause();
128. Log.e("ActivityState","onPause");
129. }
130. @Override
131. protectedvoidonResume(){
132. //TODOAuto-generatedmethodstub
133. super.onResume();
134. Log.e("ActivityState","onResume");
135. }
136. }