In the previous article, we introduced how to create and respond to option menus in Android. Today we will explore sub-menus and Icon menus.
Sub menu
A sub-Menu provides a natural way to organize menu items. It is widely used in GUI Design of windows and other operating systems. Android also supports sub-menus. You can easily create and respond to sub-menus through the addsubmenu (INT groupid, int Itemid, int order, int titleres) method.
@ Override
Publicboolean oncreateoptionsmenu (menu ){
Int base = menu. first;
// A menu can contain multiple sub menus
Submenu = menu. addsubmenu (base, base + 1, menu. None, "system settings ");
// The Sub-menu can contain multiple menu items
Menuitem menuitem1 = submenu. Add (base, base + 1, base + 1, "Display Settings ");
Submenu. Add (base, base + 2, base + 2, "Network Settings ");
Submenu. Add (base, base + 3, base + 3, "Advanced Settings ");
Submenu. Add (base, base + 4, base + 4, "Security Settings ");
// The Sub-menu item does not support displaying icons. This is meaningless, although no error is reported!
Menuitem1.seticon (R. drawable. displaysettings );
// But the sub-menu itself supports icons
Submenu. seticon (R. drawable. settings );
// Return true if the menu is displayed.
Returntrue;
}
The code above demonstrates how to create a sub-menu. The response of the menu item is actually the response of the normal menu item. The previous article has already described it in detail and will not go into details here. In Android, you can add icons for sub-menus,The icon of its menu item is not displayed. In addition to the seticon (INT iconres) method in the Code, the setheadericon (INT iconres) method can be used to add the title icon of the sub-menu item column. The effect is shown in the third figure above.
It should be emphasized that the menu can contain multiple submenus, And the submenu can contain multiple menuitems (for details about the relationship between the three, see the android menu (1)-understanding the menu in Android ), however, submenu cannot contain submenu, andSubmenus cannot be nested.!!! The following code can be compiled, but errors may occur during running.
Submenu. addsubmenu ("another sub menu ");
Icon menu
Android supports displaying various icons on the menu, which is used when you create a sub-menu. The icon menu is strictly not a new type of menu, and its usage is also very simple. The reason for setting a separate section is to illustrate some restrictions on using the icon. In Android, not all menu items can be added, but the following menu items are not allowed (this does not mean that the program reports an error, but the runtime icon is not displayed ):
- Extended menu items. If you do not understand what an extended menu is, you can refer to the android menu for details (1)-understand the menu in Android
- Sub-menu items
- Context menu items
In addition, check mark cannot be added to menu items with icons ). In short, although exquisite Icons can add a lot of colors to our applications, abuse of icons is also counterproductive, the restrictions added by the android SDK to the icon menu are also effective to prevent icon abuse.
Conclusion
This topic describes the sub-menus in Android and notes for adding icons to menu items. The next topic "android menu details (IV) -- use context menu contextmenu to introduce the use of context menu.