菜單開發也是程序員經常需要關注的,如何使菜單變得更加方便用戶使用是每一個開發者都要面臨的實際問題,今天給android開發者介紹一下如何開發出像UC浏覽器一樣的菜單。使用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;
}
}