欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android封装的menu自定义菜单列表

程序员文章站 2022-07-15 12:45:51
...

自己封装的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返回的结果   
        }  
    }  
}

 

 

<?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>
 

 

<?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>