Android用PopupWindow实现弹出菜单实例
step1:新建项目PopWindow,并导入菜单项使用的图片到/res/drawable目录下
(项目总览图) (drawable目录截图)
step2:设置应用的UI界面
a.应用的总体界面,main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="openPopWindow"
/>
</LinearLayout>
b.弹出菜单的界面,popwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/rectangle"> <!-- 设置一个手绘的长方形背景 -->
<GridView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="4"
android:horizontalSpacing="10dp" android:verticalSpacing="10dp"
android:id="@+id/gridView" />
</LinearLayout>
其中的android:background="@drawable/rectangle"是引用rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#1DC9CD" android:endColor="#A2E0FB"
android:angle="270" />
<padding android:left="2dp" android:top="2dp" android:right="2dp"
android:bottom="2dp" />
</shape>