使用AlertDialog生成菜單,利用setView()方法設置菜單視圖。
布局如下:
1、菜單布局
利用GridView
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:orientation="vertical">
- <GridView android:id="@+id/menu"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:numColumns="2"
- android:verticalSpacing="5dip"
- android:horizontalSpacing="5dip"
- android:stretchMode="columnWidth"
- android:gravity="center"></GridView>
- </LinearLayout>
2、每一個item的布局
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RelativeLayout_Item" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:paddingBottom="5dip">
- <ImageView android:id="@+id/item_image"
- android:layout_centerHorizontal="true" android:layout_width="wrap_content"
- android:layout_height="wrap_content"></ImageView>
- <TextView android:layout_below="@id/item_image" android:id="@+id/item_text"
- android:layout_centerHorizontal="true" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="選項"></TextView>
- </RelativeLayout>
主程序代碼如下:
代碼比較簡單,注釋非常詳細
- package com.cloay.down.utils;
-
- import java.util.ArrayList;
- import java.util.HashMap;
-
- import com.cloay.down.R;
-
- import android.app.AlertDialog;
- import android.content.Context;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.SimpleAdapter;
- /**
- * 菜單工具類
- * MenuUtil.java
- * @author cloay
- * 2011-10-25
- */
- public class MenuUtil {
- private static AlertDialog menuDialog;// menu菜單Dialog
- private static GridView menuGrid;
-
- /** 菜單圖片 **/
- static int[] menu_image_array = { R.drawable.menu_open_in_background, R.drawable.menu_redownload, R.drawable.menu_detail, R.drawable.menu_delete };
- /** 菜單文字 **/
- static String[] menu_name_array = { "打開", "重新下載", "詳細", "刪除"};
- public static void ShowMenuDialog(final Context context){
- View menuView = View.inflate(context, R.layout.menu, null);
- menuDialog = new AlertDialog.Builder(context)
- .setView(menuView)
- .create();
- menuDialog.show();
- menuGrid = (GridView) menuView.findViewById(R.id.menu);
- menuGrid.setAdapter(getMenuAdapter(context, menu_name_array, menu_image_array));
- menuGrid.setOnItemClickListener(new OnItemClickListener() {
- //監聽menu按鈕事件
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- switch(position){
- case 0: //open file
-
- break;
- case 1: //redownload 重新下載
-
- break;
- case 2: //file details
-
- break;
- case 3: //delete file
-
- break;
- }
- }
- });
- }
-
- /**
- * 為menuGrid設置Adapter
- * @param context
- * @param menuNameArray
- * @param imageResourceArray
- * @return
- */
- private static SimpleAdapter getMenuAdapter(Context context, String[] menuNameArray,
- int[] imageResourceArray) {
- ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
- for (int i = 0; i < menuNameArray.length; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("itemImage", imageResourceArray[i]);
- map.put("itemText", menuNameArray[i]);
- data.add(map);
- }
- SimpleAdapter simperAdapter = new SimpleAdapter(context, data,
- R.layout.item_menu, new String[] { "itemImage", "itemText" },
- new int[] { R.id.item_image, R.id.item_text });
- return simperAdapter;
- }
- }