转自:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html
菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),今天这讲是OptionsMenu
一、概述
public boolean onCreateOptionsMenu(Menu menu):使用此方法调用OptionsMenu 。
public boolean onOptionsItemSelected(MenuItem item):选中菜单项后发生的动作。
public void onOptionsMenuClosed(Menu menu):菜单关闭后发生的动作。
public boolean onPrepareOptionsMenu(Menu menu):选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单。
public boolean onMenuOpened(int featureId, Menu menu):单打开后发生的动作。
二、默认样式
默认样式是在屏幕底部弹出一个菜单,这个菜单我们就叫他选项菜单OptionsMenu,一般情况下,选项菜单最多显示2排每排3个菜单项,这些菜单项有文字有图标,也被称作Icon Menus,如果多于6项,从第六项开始会被隐藏,在第六项会出现一个More里,点击More才出现第六项以及以后的菜单项,这些菜单项也被称作Expanded Menus。下面介绍。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 1.main.xml 2 3 <?xml version="1.0" encoding="utf-8"?> 4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" android:layout_width="fill_parent" 6 android:layout_height="fill_parent" > 7 8 <TextView android:layout_width="wrap_content" 9 android:layout_height="wrap_content" android:text="请点击 Menu键显示选项菜单" 10 android:id="@+id/TextView02" /> 11 12 </LinearLayout>
2。重载onCreateOptionsMenu(Menu menu)方法
重载onCreateOptionsMenu(Menu menu)方法,并在此方法中添加菜单项,最后返回true,如果false,菜单则不会显示。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public boolean onCreateOptionsMenu(Menu menu) 2 3 @Override 4 public boolean onCreateOptionsMenu(Menu menu) { 5 /* 6 * 7 * add()方法的四个参数,依次是: 8 * 9 * 1、组别,如果不分组的话就写Menu.NONE, 10 * 11 * 2、Id,这个很重要,Android根据这个Id来确定不同的菜单 12 * 13 * 3、顺序,那个菜单现在在前面由这个参数的大小决定 14 * 15 * 4、文本,菜单的显示文本 16 */ 17 18 menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon( 19 20 android.R.drawable.ic_menu_delete); 21 22 // setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以 23 24 // android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的 25 26 menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon( 27 28 android.R.drawable.ic_menu_edit); 29 30 menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon( 31 32 android.R.drawable.ic_menu_help); 33 34 menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon( 35 36 android.R.drawable.ic_menu_add); 37 38 menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon( 39 40 android.R.drawable.ic_menu_info_details); 41 42 menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon( 43 44 android.R.drawable.ic_menu_send); 45 46 return true; 47 48 }
3。为菜单项注册事件
使用onOptionsItemSelected(MenuItem item)方法为菜单项注册事件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public boolean onOptionsItemSelected(MenuItem item) 2 3 @Override 4 public boolean onOptionsItemSelected(MenuItem item) { 5 switch (item.getItemId()) { 6 7 case Menu.FIRST + 1: 8 9 Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show(); 10 11 break; 12 13 case Menu.FIRST + 2: 14 15 Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show(); 16 17 break; 18 19 case Menu.FIRST + 3: 20 21 Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show(); 22 23 break; 24 25 case Menu.FIRST + 4: 26 27 Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show(); 28 29 break; 30 31 case Menu.FIRST + 5: 32 33 Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show(); 34 35 break; 36 37 case Menu.FIRST + 6: 38 39 Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show(); 40 41 break; 42 43 } 44 45 return false; 46 47 }
4。其他按需要重载
完整代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 DefaultMenu 2 3 package com.wjq.menu; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.widget.Toast; 10 11 public class DefaultMenu extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 } 18 19 @Override 20 public boolean onCreateOptionsMenu(Menu menu) { 21 /* 22 * 23 * add()方法的四个参数,依次是: 24 * 25 * 1、组别,如果不分组的话就写Menu.NONE, 26 * 27 * 2、Id,这个很重要,Android根据这个Id来确定不同的菜单 28 * 29 * 3、顺序,那个菜单现在在前面由这个参数的大小决定 30 * 31 * 4、文本,菜单的显示文本 32 */ 33 34 menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon( 35 36 android.R.drawable.ic_menu_delete); 37 38 // setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以 39 40 // android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的 41 42 menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon( 43 44 android.R.drawable.ic_menu_edit); 45 46 menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon( 47 48 android.R.drawable.ic_menu_help); 49 50 menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon( 51 52 android.R.drawable.ic_menu_add); 53 54 menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon( 55 56 android.R.drawable.ic_menu_info_details); 57 58 menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon( 59 60 android.R.drawable.ic_menu_send); 61 62 return true; 63 64 } 65 66 @Override 67 public boolean onOptionsItemSelected(MenuItem item) { 68 switch (item.getItemId()) { 69 70 case Menu.FIRST + 1: 71 72 Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show(); 73 74 break; 75 76 case Menu.FIRST + 2: 77 78 Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show(); 79 80 break; 81 82 case Menu.FIRST + 3: 83 84 Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show(); 85 86 break; 87 88 case Menu.FIRST + 4: 89 90 Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show(); 91 92 break; 93 94 case Menu.FIRST + 5: 95 96 Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show(); 97 98 break; 99 100 case Menu.FIRST + 6: 101 102 Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show(); 103 104 break; 105 106 } 107 108 return false; 109 110 } 111 112 @Override 113 public void onOptionsMenuClosed(Menu menu) { 114 Toast.makeText(this, "选项菜单关闭了", Toast.LENGTH_LONG).show(); 115 } 116 117 @Override 118 public boolean onPrepareOptionsMenu(Menu menu) { 119 Toast.makeText(this, 120 "选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单", 121 Toast.LENGTH_LONG).show(); 122 123 // 如果返回false,此方法就把用户点击menu的动作给消费了,onCreateOptionsMenu方法将不会被调用 124 125 return true; 126 127 } 128 }
5.效果浏览
三、自定义样式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 1.gridview_menu.xml 2 3 <?xml version="1.0" encoding="utf-8"?> 4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" 6 android:layout_width="fill_parent" 7 android:layout_height="fill_parent" 8 > 9 <GridView 10 android:id="@+id/gridview" 11 android:layout_width="fill_parent" 12 android:layout_height="fill_parent" 13 android:numColumns="4" 14 android:verticalSpacing="10dip" 15 android:horizontalSpacing="10dip" 16 android:stretchMode="columnWidth" 17 android:gravity="center" 18 /> 19 20 </LinearLayout>
首先自定义菜单界面,我是GridView来包含菜单项,4列3行
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 2.item_menu.xml 2 3 <?xml version="1.0" encoding="utf-8"?> 4 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 android:id="@+id/RelativeLayout_Item" 6 android:layout_width="fill_parent" android:layout_height="wrap_content" 7 android:paddingBottom="5dip"> 8 <ImageView android:id="@+id/item_image" 9 android:layout_centerHorizontal="true" android:layout_width="wrap_content" 10 android:layout_height="wrap_content"></ImageView> 11 <TextView android:layout_below="@id/item_image" android:id="@+id/item_text" 12 android:layout_centerHorizontal="true" android:layout_width="wrap_content" 13 android:layout_height="wrap_content" android:text="选项"></TextView> 14 </RelativeLayout>
菜单项的现实样式,一个图标和一个文字。
3.定义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 代码 2 3 private boolean isMore = false;// menu菜单翻页控制 4 AlertDialog menuDialog;// menu菜单Dialog 5 GridView menuGrid; 6 View menuView; 7 8 private final int ITEM_SEARCH = 0;// 搜索 9 private final int ITEM_FILE_MANAGER = 1;// 文件管理 10 private final int ITEM_DOWN_MANAGER = 2;// 下载管理 11 private final int ITEM_FULLSCREEN = 3;// 全屏 12 private final int ITEM_MORE = 11;// 菜单 13 14 15 /** 菜单图片 **/ 16 int[] menu_image_array = { R.drawable.menu_search, 17 R.drawable.menu_filemanager, R.drawable.menu_downmanager, 18 R.drawable.menu_fullscreen, R.drawable.menu_inputurl, 19 R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import, 20 R.drawable.menu_sharepage, R.drawable.menu_quit, 21 R.drawable.menu_nightmode, R.drawable.menu_refresh, 22 R.drawable.menu_more }; 23 /** 菜单文字 **/ 24 String[] menu_name_array = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签", 25 "加入书签", "分享页面", "退出", "夜间模式", "刷新", "更多" }; 26 /** 菜单图片2 **/ 27 int[] menu_image_array2 = { R.drawable.menu_auto_landscape, 28 R.drawable.menu_penselectmodel, R.drawable.menu_page_attr, 29 R.drawable.menu_novel_mode, R.drawable.menu_page_updown, 30 R.drawable.menu_checkupdate, R.drawable.menu_checknet, 31 R.drawable.menu_refreshtimer, R.drawable.menu_syssettings, 32 R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return }; 33 /** 菜单文字2 **/ 34 String[] menu_name_array2 = { "自动横屏", "笔选模式", "阅读模式", "浏览模式", "快捷翻页", 35 "检查更新", "检查网络", "定时刷新", "设置", "帮助", "关于", "返回" };
如果第一次打开则设置视图,否则直接显示menuDialog视图。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 5.private SimpleAdapter getMenuAdapter(String[] menuNameArray, 2 3 private SimpleAdapter getMenuAdapter(String[] menuNameArray, 4 int[] imageResourceArray) { 5 ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); 6 for (int i = 0; i < menuNameArray.length; i++) { 7 HashMap<String, Object> map = new HashMap<String, Object>(); 8 map.put("itemImage", imageResourceArray[i]); 9 map.put("itemText", menuNameArray[i]); 10 data.add(map); 11 } 12 SimpleAdapter simperAdapter = new SimpleAdapter(this, data, 13 R.layout.item_menu, new String[] { "itemImage", "itemText" }, 14 new int[] { R.id.item_image, R.id.item_text }); 15 return simperAdapter; 16 }
为菜单添加菜单项。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @Override 2 public boolean onCreateOptionsMenu(Menu menu) { 3 menu.add("menu");// 必须创建一项 4 return super.onCreateOptionsMenu(menu); 5 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 7.protected void onCreate(Bundle savedInstanceState) 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 // TODO Auto-generated method stub 6 super.onCreate(savedInstanceState); 7 8 setContentView(R.layout.main); 9 10 menuView = View.inflate(this, R.layout.gridview_menu, null); 11 // 创建AlertDialog 12 menuDialog = new AlertDialog.Builder(this).create(); 13 menuDialog.setView(menuView); 14 menuDialog.setOnKeyListener(new OnKeyListener() { 15 public boolean onKey(DialogInterface dialog, int keyCode, 16 KeyEvent event) { 17 if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键 18 dialog.dismiss(); 19 return false; 20 } 21 }); 22 23 menuGrid = (GridView) menuView.findViewById(R.id.gridview); 24 menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array)); 25 /** 监听menu选项 **/ 26 menuGrid.setOnItemClickListener(new OnItemClickListener() { 27 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 28 long arg3) { 29 switch (arg2) { 30 case ITEM_SEARCH:// 搜索 31 32 break; 33 case ITEM_FILE_MANAGER:// 文件管理 34 35 break; 36 case ITEM_DOWN_MANAGER:// 下载管理 37 38 break; 39 case ITEM_FULLSCREEN:// 全屏 40 41 break; 42 case ITEM_MORE:// 翻页 43 if (isMore) { 44 menuGrid.setAdapter(getMenuAdapter(menu_name_array2, 45 menu_image_array2)); 46 isMore = false; 47 } else {// 首页 48 menuGrid.setAdapter(getMenuAdapter(menu_name_array, 49 menu_image_array)); 50 isMore = true; 51 } 52 menuGrid.invalidate();// 更新menu 53 menuGrid.setSelection(ITEM_MORE); 54 break; 55 } 56 57 58 } 59 }); 60 }
完整代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 完整代码 2 3 package com.wjq.menu; 4 5 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 9 import android.app.Activity; 10 import android.app.AlertDialog; 11 import android.content.DialogInterface; 12 import android.content.DialogInterface.OnKeyListener; 13 import android.os.Bundle; 14 import android.view.KeyEvent; 15 import android.view.Menu; 16 import android.view.View; 17 import android.widget.AdapterView; 18 import android.widget.GridView; 19 import android.widget.SimpleAdapter; 20 import android.widget.AdapterView.OnItemClickListener; 21 22 public class CustomizeMenu extends Activity { 23 24 private boolean isMore = false;// menu菜单翻页控制 25 AlertDialog menuDialog;// menu菜单Dialog 26 GridView menuGrid; 27 View menuView; 28 29 private final int ITEM_SEARCH = 0;// 搜索 30 private final int ITEM_FILE_MANAGER = 1;// 文件管理 31 private final int ITEM_DOWN_MANAGER = 2;// 下载管理 32 private final int ITEM_FULLSCREEN = 3;// 全屏 33 private final int ITEM_MORE = 11;// 菜单 34 35 36 /** 菜单图片 **/ 37 int[] menu_image_array = { R.drawable.menu_search, 38 R.drawable.menu_filemanager, R.drawable.menu_downmanager, 39 R.drawable.menu_fullscreen, R.drawable.menu_inputurl, 40 R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import, 41 R.drawable.menu_sharepage, R.drawable.menu_quit, 42 R.drawable.menu_nightmode, R.drawable.menu_refresh, 43 R.drawable.menu_more }; 44 /** 菜单文字 **/ 45 String[] menu_name_array = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签", 46 "加入书签", "分享页面", "退出", "夜间模式", "刷新", "更多" }; 47 /** 菜单图片2 **/ 48 int[] menu_image_array2 = { R.drawable.menu_auto_landscape, 49 R.drawable.menu_penselectmodel, R.drawable.menu_page_attr, 50 R.drawable.menu_novel_mode, R.drawable.menu_page_updown, 51 R.drawable.menu_checkupdate, R.drawable.menu_checknet, 52 R.drawable.menu_refreshtimer, R.drawable.menu_syssettings, 53 R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return }; 54 /** 菜单文字2 **/ 55 String[] menu_name_array2 = { "自动横屏", "笔选模式", "阅读模式", "浏览模式", "快捷翻页", 56 "检查更新", "检查网络", "定时刷新", "设置", "帮助", "关于", "返回" }; 57 @Override 58 protected void onCreate(Bundle savedInstanceState) { 59 // TODO Auto-generated method stub 60 super.onCreate(savedInstanceState); 61 62 setContentView(R.layout.main); 63 64 menuView = View.inflate(this, R.layout.gridview_menu, null); 65 // 创建AlertDialog 66 menuDialog = new AlertDialog.Builder(this).create(); 67 menuDialog.setView(menuView); 68 menuDialog.setOnKeyListener(new OnKeyListener() { 69 public boolean onKey(DialogInterface dialog, int keyCode, 70 KeyEvent event) { 71 if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键 72 dialog.dismiss(); 73 return false; 74 } 75 }); 76 77 menuGrid = (GridView) menuView.findViewById(R.id.gridview); 78 menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array)); 79 /** 监听menu选项 **/ 80 menuGrid.setOnItemClickListener(new OnItemClickListener() { 81 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 82 long arg3) { 83 switch (arg2) { 84 case ITEM_SEARCH:// 搜索 85 86 break; 87 case ITEM_FILE_MANAGER:// 文件管理 88 89 break; 90 case ITEM_DOWN_MANAGER:// 下载管理 91 92 break; 93 case ITEM_FULLSCREEN:// 全屏 94 95 break; 96 case ITEM_MORE:// 翻页 97 if (isMore) { 98 menuGrid.setAdapter(getMenuAdapter(menu_name_array2, 99 menu_image_array2)); 100 isMore = false; 101 } else {// 首页 102 menuGrid.setAdapter(getMenuAdapter(menu_name_array, 103 menu_image_array)); 104 isMore = true; 105 } 106 menuGrid.invalidate();// 更新menu 107 menuGrid.setSelection(ITEM_MORE); 108 break; 109 } 110 111 112 } 113 }); 114 } 115 @Override 116 public boolean onCreateOptionsMenu(Menu menu) { 117 menu.add("menu");// 必须创建一项 118 return super.onCreateOptionsMenu(menu); 119 } 120 121 private SimpleAdapter getMenuAdapter(String[] menuNameArray, 122 int[] imageResourceArray) { 123 ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); 124 for (int i = 0; i < menuNameArray.length; i++) { 125 HashMap<String, Object> map = new HashMap<String, Object>(); 126 map.put("itemImage", imageResourceArray[i]); 127 map.put("itemText", menuNameArray[i]); 128 data.add(map); 129 } 130 SimpleAdapter simperAdapter = new SimpleAdapter(this, data, 131 R.layout.item_menu, new String[] { "itemImage", "itemText" }, 132 new int[] { R.id.item_image, R.id.item_text }); 133 return simperAdapter; 134 } 135 @Override 136 public boolean onMenuOpened(int featureId, Menu menu) { 137 if (menuDialog == null) { 138 menuDialog = new AlertDialog.Builder(this).setView(menuView).show(); 139 } else { 140 menuDialog.show(); 141 } 142 return false;// 返回为true 则显示系统menu 143 } 144 145 }
效果浏览