Submenus
A sub menu can be added within any menu, except another sub menu. These are very useful when your application has a lot of functions that may be organized in topics, like the items in a PC application's menu bar (File, Edit, View, etc.).
A sub menu is created by adding it to an existing Menu
with
. This returns a addSubMenu()
SubMenu
object (an extension ofMenu
). You can then add additional items to this menu, with the normal routine, using the
methods. For example:add()
- public boolean onCreateOptionsMenu(Menu menu) {
- boolean result = super.onCreateOptionsMenu(menu);
- //设置子菜单的名称
- SubMenu fileMenu = menu.addSubMenu("File");
- SubMenu editMenu = menu.addSubMenu("Edit");
- //按对应的名称增加子菜单
- fileMenu.add("new");
- fileMenu.add("open");
- fileMenu.add("save");
- editMenu.add("undo");
- editMenu.add("redo");
- return result;
- }