1 PopupWindow 需要一个自定义的布局文件
2 列如:popupwindow.xml
3
4 <?xml version="1.0" encoding="utf-8"?>
5 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 android:background="#000"
9 android:orientation="vertical" >
10 <TextView
11 android:textSize="25dp"
12 android:textColor="#ffffff"
13 android:id="@+id/textView1"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:text="@string/start" />
17
18 <TextView
19 android:textSize="25dp"
20 android:textColor="#ffffff"
21 android:id="@+id/textView2"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content"
24 android:text="@string/uninstall" />
25
26 <TextView
27 android:textSize="25dp"
28 android:textColor="#ffffff"
29 android:id="@+id/textView3"
30 android:layout_width="fill_parent"
31 android:layout_height="wrap_content"
32 android:text="@string/detil" />
33
34 </LinearLayout>
35
36 LayoutInflater flater=LayoutInflater.from(context);
37 View popview=flater.inflate(R.layout.popuwindow, null);
38 PopupWindow popup=new PopupWindow(popview, 200, 130,true);
39 第一个参数:是自定义的布局文件
40 第二、三个参数:显示的宽、高
41 第四个参数:是否获得焦点
42 popup.showAsDropDown(view);显示在view(控件)的下面,
43
//创建一个自适应大小的弹出菜单
44 new PopupWindow(popview, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true)
45 //关闭popuwindow窗口
popup.dismiss();
47 要想实现弹出的菜单的里面的菜单项的事件功能,就必须使用popview.findViewById(R.id.textview1)这样的方法之后才能为其添加事件监听器