---
title: "[Android实例] popupwindow实现弹出菜单效果"
author: "mmdev"
date: "2013-01-25T15:33:00Z"
tags: []
slug: "android-popupwindow"
draft: false
lastmod: "2013-01-29T10:47:38Z"
id: 1777504
url: "https://mmdev.iteye.com/blog/1777504"
---

![](http://www.eoeandroid.com/data/attachment/forum/201107/27/195246dzah0jaav00gazig.jpg)  
上面图片中的确定按钮的效果。  
也就是弹出菜单。  
主activity布局

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:id="@+id/com\_example\_menueffect\_effect1\_relativeLayout"
04. android:layout\_height="match\_parent"
05. android:layout\_width="match\_parent"&gt;
06. &lt;ListView android:layout\_height="wrap\_content" android:id="@+id/com\_example\_menueffect\_effect1\_listView1"
07. android:layout\_alignTop="@id/com\_example\_menueffect\_effect1\_relativeLayout"
08. android:layout\_width="match\_parent"&gt;&lt;/ListView&gt;
09. &lt;Button android:id="@+id/com\_example\_menueffect\_effect1\_btnOk"
10. android:layout\_width="wrap\_content"
11. android:layout\_height="wrap\_content"
12. android:text="确定"
13. android:layout\_alignParentBottom="true"
14. android:layout\_alignParentLeft="true"&gt;&lt;/Button&gt;
15. &lt;Button android:id="@+id/com\_example\_menueffect\_effect1\_btnCancel"
16. android:layout\_width="wrap\_content"
17. android:layout\_height="wrap\_content"
18. android:text="取消"
19. android:layout\_alignParentBottom="true"
20. android:layout\_alignParentRight="true"
21. &gt;&lt;/Button&gt;
22. &lt;/RelativeLayout&gt;

复制代码

popupmenu布局

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;LinearLayout
03. xmlns:android="http://schemas.android.com/apk/res/android"
04. android:orientation="vertical"
05. android:layout\_width="wrap\_content"
06. android:layout\_height="wrap\_content"&gt;
07. &lt;Button android:text="打开"
08. android:id="@+id/com\_example\_menueffect\_effect1\_popupwindow\_btnOpen" android:layout\_width="wrap\_content" android:layout\_height="wrap\_content"&gt;&lt;/Button&gt;
09. &lt;Button android:text="保存"
10. android:id="@+id/com\_example\_menueffect\_effect1\_popupwindow\_btnSave" android:layout\_width="wrap\_content" android:layout\_height="wrap\_content"&gt;&lt;/Button&gt;
11. &lt;Button android:text="退出"
12. android:id="@+id/com\_example\_menueffect\_effect1\_popupwindow\_btnExit" android:layout\_width="wrap\_content" android:layout\_height="wrap\_content"&gt;&lt;/Button&gt;
13. &lt;/LinearLayout&gt;

复制代码

主activity代码

01. package com.example.menueffect;
02. import android.app.Activity;
03. import android.os.Bundle;
04. import android.view.Gravity;
05. import android.view.LayoutInflater;
06. import android.view.View;
07. import android.view.View.OnClickListener;
08. import android.widget.ArrayAdapter;
09. import android.widget.Button;
10. import android.widget.ListView;
11. import android.widget.PopupWindow;
12. public class Effect1Activity extends Activity {
13. Button m\_btnOk;
14. Button m\_btnCancel;
15. ListView m\_lv;
16. String\[] m\_users;
17. PopupWindow m\_popupWindow;
18. @Override
19. protected void onCreate(Bundle savedInstanceState) {
20. // TODO Auto-generated method stub
21. super.onCreate(savedInstanceState);
22. setContentView(com.example.R.layout.com\_example\_menueffect\_effect1);
23. m\_users = new String\[] { "ssssss", "bbbbbbb", "sfdsdfsdfsfd" };
24. m\_btnOk = (Button) findViewById(com.example.R.id.com\_example\_menueffect\_effect1\_btnOk);
25. m\_btnCancel = (Button) findViewById(com.example.R.id.com\_example\_menueffect\_effect1\_btnCancel);
26. m\_lv = (ListView) findViewById(com.example.R.id.com\_example\_menueffect\_effect1\_listView1);
27. ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(this,
28. android.R.layout.simple\_list\_item\_1, m\_users);
29. m\_lv.setAdapter(arrayAdapter);
30. m\_btnOk.setOnClickListener(new OnClickListener() {
31. @Override
32. public void onClick(View v) {
33. // TODO Auto-generated method stub
34. LayoutInflater layoutInflater = getLayoutInflater();
35. View view = layoutInflater
36. .inflate(
37. com.example.R.layout.com\_example\_menueffect\_effect1\_popupwindow,
38. null);
39. m\_popupWindow=new PopupWindow(view,100, 200);
40. m\_popupWindow.showAsDropDown(m\_btnOk,0,m\_btnOk.getHeight());
41. }
42. });
43. }
44. }

\------------------------------------------------------------------------------------------------------------------------------

更多内容，查看：

[http://orgcent.com/android-popupwindow-usage/](http://orgcent.com/android-popupwindow-usage/)

[http://orgcent.com/android-popupwindow-showasdropdown-showatlocation/](http://orgcent.com/android-popupwindow-showasdropdown-showatlocation/)

## Android下PopupWindow隐藏及显示(showAtLocation/showAsDropDown)

作者：lenomon 发布：2012-04-17 11:09 分类：[Android](http://orgcent.com/category/android/ "查看 Android 中的全部文章") 阅读：111,298 [1条评论](http://orgcent.com/android-popupwindow-showasdropdown-showatlocation/#comments "《Android下PopupWindow隐藏及显示(showAtLocation/showAsDropDown)》上的评论")

[![](http://v2.jiathis.com/code_mini/images/btn/v1/jiathis1.gif)](http://www.jiathis.com/share?uid=906312)

上一篇对PopupWindow的用法（位置、动画、焦点）做了详细介绍，具体查看[Android中PopupWindow的用法(位置、动画、焦点)](http://orgcent.com/android-popupwindow-usage/ "Android中PopupWindow的用法(位置、动画、焦点)")。下面说说PopupWindow的如何隐藏、显示及显示位置（showAtLocation/showAsDropDown）。

1、PopupWindow的隐藏

1  
2  
3  
4

finalPopupWindow window=mPageStatWin;  
if(null!=window&&window.isShowing()){  
win.dismiss();  
}

2、PopupWindow的显示及位置设置

1

window.showAtLocation(parent, Gravity.RIGHT|Gravity.BOTTOM,10,10);

第一个参数指定PopupWindow的锚点view，即依附在哪个view上。  
第二个参数指定起始点为parent的右下角，第三个参数设置以parent的右下角为原点，向左、上各偏移10像素。

1  
2  
3  
4

//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示  
window.showAsDropDown(anchor);  
//xoff,yoff基于anchor的左下角进行偏移。  
window.showAsDropDown(anchor, xoff, yoff);

如果没有充足的空间显示PopupWindow，那么PopupWindow的左下角将位于anchor的左上角来显示。

转载请注明地址:[http://orgcent.com/android-popupwindow-showasdropdown-showatlocation/ | 萝卜白菜的博客](http://orgcent.com/android-popupwindow-showasdropdown-showatlocation/ "Android下PopupWindow隐藏及显示(showAtLocation/showAsDropDown)")