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>