自己封裝的menu自訂菜單列表,首先必須建立一個類繼承activity,讓後要調用自訂菜單的類直接繼承。
package com.cng;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class MenuActivityAdapter extends Activity
{
AlertDialog menuDialog;// menu菜單Dialog
GridView menuGrid;
View menuView;
final private int menuSettings=Menu.FIRST;
private static final int REQ_SYSTEM_SETTINGS = 0;
/** 菜單圖片 **/
int[] menu_image_array = { R.drawable.xuetang_light,R.drawable.tizhong_light,
R.drawable.maibo_light,
R.drawable.tiwen_light, R.drawable.xueyang_light,
R.drawable.naodian_light,R.drawable.xindian_light,
R.drawable.xueya_light,
R.drawable.denglu_light,
R.drawable.shezhi_light};
/** 菜單文字 **/
String[] menu_name_array = { "", "", "", "", "", "",
"", "", "", ""};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);女裝品牌熱門排行榜
menuView = View.inflate(this, R.layout.gridview_menu, null);
// 建立AlertDialog
menuDialog = new AlertDialog.Builder(this).create();
menuDialog.setView(menuView);
menuDialog.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)// 監聽按鍵
dialog.dismiss();
return false;
}
});
menuGrid = (GridView) menuView.findViewById(R.id.gridview);
menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
/** 監聽menu選項 **/
menuGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent=new Intent();
switch (arg2) {
case 0:// 搜尋
intent.setClass(MenuActivityAdapter.this, GlucoseList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 1:// 搜尋
intent.setClass(MenuActivityAdapter.this, WeightList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 2:// 搜尋
intent.setClass(MenuActivityAdapter.this, MaiboList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 3:// 搜尋
intent.setClass(MenuActivityAdapter.this, TiwenList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 4:// 搜尋
intent.setClass(MenuActivityAdapter.this, XueyangList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 5:// 搜尋
intent.setClass(MenuActivityAdapter.this, NaodianList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 6:// 搜尋
intent.setClass(MenuActivityAdapter.this, xinlv.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 7:// 搜尋
intent.setClass(MenuActivityAdapter.this, XueyaList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 8:// 搜尋
intent.setClass(MenuActivityAdapter.this, DengluList.class);
startActivity(intent);
menuDialog.dismiss();
break;
case 9:// 搜尋
startActivityForResult(new Intent(MenuActivityAdapter.this, Settings.class), REQ_SYSTEM_SETTINGS);
menuDialog.dismiss();
break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");// 必須建立一項
return super.onCreateOptionsMenu(menu);
}
private SimpleAdapter getMenuAdapter(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(this, data,
R.layout.item_menu, new String[] { "itemImage", "itemText" },
new int[] { R.id.item_image, R.id.item_text });
return simperAdapter;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menuDialog == null) {
menuDialog = new AlertDialog.Builder(this).setView(menuView).show();
} else {
menuDialog.show();
}
return false;// 返回為true 則顯示系統menu
}
//Settings設定介面返回的結果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_SYSTEM_SETTINGS)
{
//擷取設定介面PreferenceActivity中各個Preference的值
String loginSwitchKey = getResources().getString(R.string.auto_login_switch_key);
String customizelist1Key = getResources().getString(R.string.customize_main_list1_key);
String serverAddressKey = getResources().getString(R.string.server_address_key);
//取得屬於整個應用程式的SharedPreferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Boolean updateSwitch = settings.getBoolean(loginSwitchKey, true);
String updateFrequency = settings.getString(customizelist1Key, "體重");
String serverAddress = settings.getString(serverAddressKey, "172.16.10.254");
//列印結果
Log.v("CheckBoxPreference_Main", updateSwitch.toString());
Log.v("ListPreference_Main", updateFrequency);
Log.v("EditTextPreference_Main", serverAddress);
}
else
{
//其他Intent返回的結果
}
}
}
Gridview_menu.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"
>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="5"
android:verticalSpacing="1dip"
android:horizontalSpacing="1dip"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
Item_menu.xml代碼
<?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="fill_parent" android:layout_height="wrap_content"
android:paddingBottom="1dip">
<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>