1、Android的Menu简介
Android系统的Menu主要有三种:OptionsMenu、ContextMenu、SubMenu
自定义Menu可参考:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html

2、OptionsMenu用法
2.1、创建Menu
按下Menu键时,屏幕底部就会弹出menu选项,提供选择。创建OptionsMenu的方法是在Activity类中复写onCreateOptionsMenu(),然后通过menu.add()方法添加各种选项,add方法的详细参数和返回值如下 :
Java代码 收藏代码
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
groupId,组别,用来划分itemid的。一般为Menu.NONE
itemId,ID。是menu识别编号,供识别menu用的,很重要
order,顺序。这个参数的大小决定菜单选项出现的先后顺序。顺序按照参数由小到大
title,显示文本

返回值为ture时,menu才有效,若是 return false;则menu不能显示,也就是说menu无效。
复写方法onCreateOptionsMenu()时,就有个 return super.onCreateOptionsMenu(menu);,默认就行了。
添加menu选项例子:

menu.add(Menu.NONE,0,0,"添加").setIcon(android.R.drawable.ic_menu_add);  

2.2、处理Menu
通过复写onOptionsItemSelected()方法即可,然后通过item.getItemId(),筛选不同的itemid,然后执行相应的方法

switch (item.getItemId()) {      case 0:          Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();          break;      default:          Toast.makeText(this, "方法还没定义", Toast.LENGTH_SHORT).show();          break;  }  return super.onOptionsItemSelected(item); 


2.3、其他方法
onPrepareOptionsMenu()方法简述
点击menu按钮时,menu显示出来前,通过复写这个方法可以定义处理事件。


onOptionsMenuClosed()简述
退出menu之后,通过复写这个方法可以定义处理事件。menu按钮再被点击,back按钮被点击,或者用户选择了menu其中的一个选项,menu都会退出。

3、SubMenu用法
SubMenu实现的是子菜单功能,如“添加”的下一级菜单为“从网络添加”和“从本地添加两个选项的Menu”

SubMenu submenu=menu.addSubMenu(Menu.NONE, 0, 0, "添加")                  .setIcon(android.R.drawable.ic_menu_add);  submenu.add(Menu.NONE, 10, 0, "从网络添加");  submenu.add(Menu.NONE, 11, 1, "从本地添加");  submenu.add()方法不能设置图标,设置了也是无效的;但是submenu本身可以设置图标,submenu.setIcon(); 
4、ContextMenu用法
长按某个View达2秒后,就弹出menu。创建的方法是复写onCreateContextMenu()方法,处理方法是onContextItemSelected()。因为是长按某一个View,所以ContextMenu要注册到目标view,才能实现。
注册方法如下(如,注册到一个EditText,长按这个EditText就可以调出Menu了),et是EditText的id

this.registerForContextMenu(findViewById(R.id.et));


onCreateContextMenu里面的add()/setIcon()方法不能添加图标,就算添加了,也不能显示出来的。在onCreateContextMenu方法里面也能设置submenu子菜单,方法参考第3点

public class MenuActivity extends Activity {      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          this.registerForContextMenu(findViewById(R.id.et));          //注册到EditText,et是它的id      }        @Override      public void onCreateContextMenu(ContextMenu menu, View v,              ContextMenuInfo menuInfo) {          // TODO Auto-generated method stub          menu.add(Menu.NONE, 0, 0, "添加");          menu.add(Menu.NONE, 1, 1, "保存");          super.onCreateContextMenu(menu, v, menuInfo);      }      @Override      public boolean onContextItemSelected(MenuItem item) {          // TODO Auto-generated method stub          switch (item.getItemId()) {          case 0:              Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();              break;          case 1:              Toast.makeText(this, "保存", Toast.LENGTH_SHORT).show();              break;          default:              Toast.makeText(this, "方法还没定义", Toast.LENGTH_SHORT).show();              break;          }          return super.onContextItemSelected(item);      }  }   
5、自定义菜单
自定义菜单参考自:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html
完整代码如下:

Grid_view_menu.xml代码  <!-- 用于定义自定义Menu的布局 -->  <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >      <GridView           android:id="@+id/gridview"           android:layout_width="fill_parent"           android:layout_height="fill_parent"           android:numColumns="4"           android:verticalSpacing="10dip"           android:horizontalSpacing="10dip"           android:stretchMode="columnWidth"           android:gravity="center"           />    </LinearLayout>   Item_menu.xml代码  <!-- 用于定义Menu单个按钮的样式 -->  <?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="5dip" >        <ImageView          android:contentDescription="@string/autoMenuIcon"          android:id="@+id/item_image"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_centerHorizontal="true" >      </ImageView>        <TextView          android:id="@+id/item_text"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_below="@id/item_image"          android:layout_centerHorizontal="true"          android:text="@string/menuText" >      </TextView>    </RelativeLayout>  
//完整代码代码 public class AndroidMenuActivity extends Activity {      /** Called when the activity is first created. */      private boolean isMore = false;// menu菜单翻页控制      View menuView;      GridView menuGrid;      AlertDialog menuDialog;// menu菜单Dialog      private final int ITEM_SEARCH = 0;// 搜索      private final int ITEM_FILE_MANAGER = 1;// 文件管理      private final int ITEM_DOWN_MANAGER = 2;// 下载管理      private final int ITEM_FULLSCREEN = 3;// 全屏      private final int ITEM_MORE = 11;// 菜单          /** 菜单图片 **/      int[] menu_image_array = {               R.drawable.menu_search,              R.drawable.menu_filemanager,               R.drawable.menu_downmanager,              R.drawable.menu_fullscreen,               R.drawable.menu_inputurl,              R.drawable.menu_bookmark,               R.drawable.menu_bookmark_sync_import,              R.drawable.menu_sharepage,               R.drawable.menu_quit,              R.drawable.menu_nightmode,               R.drawable.menu_refresh,              R.drawable.menu_more               };      /** 菜单文字 **/      String[] menu_name_array = {               "搜索",               "文件管理",               "下载管理",               "全屏",               "网址",               "书签",              "加入书签",               "分享页面",               "退出",               "夜间模式",               "刷新",               "更多"               };      /** 菜单图片2 **/      int[] menu_image_array2 = {               R.drawable.menu_auto_landscape,              R.drawable.menu_penselectmodel,               R.drawable.menu_page_attr,              R.drawable.menu_novel_mode,               R.drawable.menu_page_updown,              R.drawable.menu_checkupdate,               R.drawable.menu_checknet,              R.drawable.menu_refreshtimer,               R.drawable.menu_syssettings,              R.drawable.menu_help,               R.drawable.menu_about,               R.drawable.menu_return               };      /** 菜单文字2 **/      String[] menu_name_array2 = {               "自动横屏",               "笔选模式",               "阅读模式",               "浏览模式",               "快捷翻页",              "检查更新",               "检查网络",               "定时刷新",               "设置",               "帮助",               "关于",               "返回"              };            @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          //通过grid_view_menu.xml          menuView = View.inflate(this, R.layout.grid_view_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) {                  // TODO Auto-generated method stub                  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) {                  switch (arg2) {                  case ITEM_SEARCH:break;                  case ITEM_FILE_MANAGER:break;                  case ITEM_DOWN_MANAGER:break;                  case ITEM_FULLSCREEN:break;                  case ITEM_MORE:// 翻页                      if (isMore) {                          menuGrid.setAdapter(getMenuAdapter(menu_name_array2,menu_image_array2));                          isMore = false;                      } else {// 首页                          menuGrid.setAdapter(getMenuAdapter(menu_name_array,menu_image_array));                          isMore = true;                      }                      menuGrid.invalidate();// 更新menu                      menuGrid.setSelection(ITEM_MORE);                      break;                  }              }          });      }      @Override      public boolean onCreateOptionsMenu(Menu menu) {          // TODO Auto-generated method stub          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      }  }    





更多相关文章

  1. Android的Btimap处理大图片解决方法
  2. JetPack 之 DataBinding 的使用
  3. Android中如何将dp,dip,sp与px相互转化
  4. Android(安卓)NDK开发基础篇(二)
  5. Android判断当前应用程序处于前台还是后台的两种方法
  6. Android的两种数据存储方式分析(一)
  7. 更新Activity的几个方法
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Android RecyclerView实现九宫格效果
  2. Android TextView加载html图片详解
  3. 第一个PhoneGap程序以及错误解决
  4. Android学习——Activity生命周期
  5. 安装 Android 2.3 could not find adb.ex
  6. Android初级工程师面试题答案——Android
  7. android全屏,隐藏标题栏
  8. Android monkey test 脚本的编写
  9. Android(安卓)Kernel Porting 的简单总结
  10. Android的Widget