Android 菜单(SubMenu)
SubMenu(子菜单),是在选项菜单的基础上增加子菜单。
一个Menu对象可以拥有0或多个SubMenu,通过调用Menu.addSubMenu方法将SubMenu添加到当前Menu中。在SubMenu 添加MenuItem的方式和在Menu中添加MenuItem方式一样,因为SubMenu是Menu的子类,但是SubMenu里不能再添加 SubMenu。
Android提供了基于Group id管理多个MenuItem的方法。
removeGroup(int group):移除所有属于group的MenuItem。
setGroupEnable(int group,boolean enable):批量开启或关闭整个组的MenuItem。
setGroupVisible(int group,visible):批量显示或隐藏整个组的MenuItem。
setGroupCheckable(int group,boolean checkable,boolean exclusive):设置菜单是否可选中。最后一个exclusive是指是单选还是多选,当exclusive是true时,系统将菜单前面添加单选 框,是false时则变成是checkbox多选框。
示例:(在选项菜单[帮助]上配置了三个子菜单[LiveHelp,Email,Phone])
效果图
Activity代码
-
- finalprivatestaticintOPTION_MENU_HELP=3;
- finalprivatestaticintOPTION_MENUS_GROUP0=0;
- finalprivatestaticintOPTION_MENUS_GROUP1=1;
- finalprivatestaticintOPTION_MENUS_GROUP2=2;
- privateMenuEntity[]optionMenus={
- newMenuEntity(OPTION_MENUS_GROUP0,1,1,"删除",android.R.drawable.ic_menu_delete),
- newMenuEntity(OPTION_MENUS_GROUP0,2,2,"保存",android.R.drawable.ic_menu_edit),
- newMenuEntity(OPTION_MENUS_GROUP0,3,3,"帮助",android.R.drawable.ic_menu_help),
- newMenuEntity(OPTION_MENUS_GROUP0,4,4,"添加",android.R.drawable.ic_menu_add),
- newMenuEntity(OPTION_MENUS_GROUP1,5,5,"详细",android.R.drawable.ic_menu_info_details),
- newMenuEntity(OPTION_MENUS_GROUP1,6,6,"发送",android.R.drawable.ic_menu_send),
- newMenuEntity(OPTION_MENUS_GROUP1,7,7,"电话",android.R.drawable.ic_menu_call),
- newMenuEntity(OPTION_MENUS_GROUP1,8,8,"照相",android.R.drawable.ic_menu_camera),
- };
-
- privateMenuEntity[]helpSubMenus={
- newMenuEntity(OPTION_MENUS_GROUP2,9,1,"LiveHelp",android.R.drawable.ic_menu_delete),
- newMenuEntity(OPTION_MENUS_GROUP2,10,2,"Email",android.R.drawable.ic_menu_edit),
- newMenuEntity(OPTION_MENUS_GROUP2,11,2,"Phone",android.R.drawable.ic_menu_edit),
- };
-
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_list_view);
- }
-
-
-
-
-
-
- publicStringreadOptionsMenu(intindex){
- for(inti=0;i<optionMenus.length;i++){
- if(optionMenus[i].getItemId()==index)returnoptionMenus[i].getTitle().toString();
- }
- for(inti=0;i<helpSubMenus.length;i++){
- if(helpSubMenus[i].getItemId()==index)returnhelpSubMenus[i].getTitle().toString();
- }
- return"";
- }
-
-
-
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- for(MenuEntitymenuEntity:optionMenus){
- if(OPTION_MENU_HELP==menuEntity.getItemId()){
-
- SubMenusubMenu=menu.addSubMenu(menuEntity.getGroupId(),menuEntity.getItemId(),menuEntity.getOrder(),menuEntity.getTitle()).setIcon(menuEntity.getIconRes());
- for(MenuEntitysubMenuEntity:helpSubMenus){
- subMenu.add(subMenuEntity.getGroupId(),subMenuEntity.getItemId(),subMenuEntity.getOrder(),subMenuEntity.getTitle()).setIcon(subMenuEntity.getIconRes());
- }
- }else{
- menu.add(menuEntity.getGroupId(),menuEntity.getItemId(),menuEntity.getOrder(),menuEntity.getTitle()).setIcon(menuEntity.getIconRes());
- }
- }
- returnsuper.onCreateOptionsMenu(menu);
- }
-
- @Override
- publicbooleanonOptionsItemSelected(MenuItemitem){
- intitemId=item.getItemId();
- Toast.makeText(context,"menu["+itemId+":"+item.getTitle()+"]被点击了",Toast.LENGTH_SHORT).show();
- returnsuper.onOptionsItemSelected(item);
- }
-
-
-
- @Override
- publicvoidonOptionsMenuClosed(Menumenu){
- Toast.makeText(context,"OptionsMenu被关闭了",Toast.LENGTH_SHORT).show();
- super.onOptionsMenuClosed(menu);
- }
备注
- privateMenuEntity[]optionMenus={
- newMenuEntity(OPTION_MENUS_GROUP0,1,1,"删除",android.R.drawable.ic_menu_delete),
- newMenuEntity(OPTION_MENUS_GROUP0,2,2,"保存",android.R.drawable.ic_menu_edit),
- newMenuEntity(OPTION_MENUS_GROUP0,3,3,"帮助",android.R.drawable.ic_menu_help),
- newMenuEntity(OPTION_MENUS_GROUP0,4,4,"添加",android.R.drawable.ic_menu_add),
- newMenuEntity(OPTION_MENUS_GROUP1,5,5,"详细",android.R.drawable.ic_menu_info_details),
- newMenuEntity(OPTION_MENUS_GROUP1,6,6,"发送",android.R.drawable.ic_menu_send),
- newMenuEntity(OPTION_MENUS_GROUP1,7,7,"电话",android.R.drawable.ic_menu_call),
- newMenuEntity(OPTION_MENUS_GROUP1,8,8,"照相",android.R.drawable.ic_menu_camera),
- };
-
- privateMenuEntity[]helpSubMenus={
- newMenuEntity(OPTION_MENUS_GROUP2,9,1,"LiveHelp",android.R.drawable.ic_menu_delete),
- newMenuEntity(OPTION_MENUS_GROUP2,10,2,"Email",android.R.drawable.ic_menu_edit),
- newMenuEntity(OPTION_MENUS_GROUP2,11,2,"Phone",android.R.drawable.ic_menu_edit),
- };
其中的
参考关于菜单基础属性的实体类
附录一 : 调用方法removeGroup(int group)
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- for(MenuEntitymenuEntity:optionMenus){
- if(OPTION_MENU_HELP==menuEntity.getItemId()){
-
- SubMenusubMenu=menu.addSubMenu(menuEntity.getGroupId(),menuEntity.getItemId(),menuEntity.getOrder(),menuEntity.getTitle()).setIcon(menuEntity.getIconRes());
- for(MenuEntitysubMenuEntity:helpSubMenus){
- subMenu.add(subMenuEntity.getGroupId(),subMenuEntity.getItemId(),subMenuEntity.getOrder(),subMenuEntity.getTitle()).setIcon(subMenuEntity.getIconRes());
- }
- }else{
- menu.add(menuEntity.getGroupId(),menuEntity.getItemId(),menuEntity.getOrder(),menuEntity.getTitle()).setIcon(menuEntity.getIconRes());
- }
- }
- menu.removeGroup(OPTION_MENUS_GROUP1);
- returnsuper.onCreateOptionsMenu(menu);
- }
如图所示:原group为1的菜单不显示了!
附录二 : 调用方法setGroupEnabled(int group, boolean enabled)
关闭或者开启组为group的全部菜单。(关闭:enabled=false,菜单项变灰,不能点;开启:enabled=false,菜单项正常,可以点)
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- for(MenuEntitymenuEntity:optionMenus){
- if(OPTION_MENU_HELP==menuEntity.getItemId()){
-
- SubMenusubMenu=menu.addSubMenu(menuEntity.getGroupId(),menuEntity.getItemId(),menuEntity.getOrder(),menuEntity.getTitle()).setIcon(menuEntity.getIconRes());
- for(MenuEntitysubMenuEntity:helpSubMenus){
- subMenu.add(subMenuEntity.getGroupId(),subMenuEntity.getItemId(),subMenuEntity.getOrder(),subMenuEntity.getTitle()).setIcon(subMenuEntity.getIconRes());
- }
- }else{
- menu.add(menuEntity.getGroupId(),menuEntity.getItemId(),menuEntity.getOrder(),menuEntity.getTitle()).setIcon(menuEntity.getIconRes());
- }
- }
- menu.setGroupEnabled(OPTION_MENUS_GROUP1,false);
- returnsuper.onCreateOptionsMenu(menu);
- }
group为1的菜单项变灰了
2011-06-15 0:51
package com.test.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
public class testmenuActivity extends Activity {
public static final int ITEM0 = Menu.FIRST;
public static final int ITEM1 = Menu.FIRST+1;
Button button1;
Button button2;
MenuItem item;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setVisibility(View.INVISIBLE);
button2.setVisibility(View.INVISIBLE);
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, ITEM0, 0, "显示button1");
menu.add(0, ITEM1, 1, "显示button2");
menu.findItem(ITEM1);
item = (MenuItem) menu.findItem(ITEM0);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case ITEM0:
actionClickMenuItem1();
bool = false;
break;
case ITEM1:
actionClickMenuItem2();
bool = true;
break;
}
return super.onOptionsItemSelected(item);
}
/*
*点击第一个menu的第一个按钮执行的动作;
*/
private void actionClickMenuItem1(){
setTitle("button1 可见");
button1.setVisibility(View.INVISIBLE);
button2.setVisibility(View.VISIBLE);
item.setIcon(R.drawable.menu_start);
}
/*
*点击第一个menu的第一个按钮执行的动作;
*/
private void actionClickMenuItem2(){
setTitle("button2 可见");
button2.setVisibility(View.INVISIBLE);
button1.setVisibility(View.VISIBLE);
item.setIcon(R.drawable.menu_end);
}
}