Menu of Android

Source: Internet
Author: User

Android provides three menu types: options menu, context menu, and sub menu.

The options menu is displayed by pressing the home key. The context menu needs to be displayed after pressing the top 2 S button on The view. Both menus can be added to sub-menus. Sub-menus cannot be nested. The options menu can display up to six menu options at the bottom of the screen, which is called the icon menu. The icon menu cannot have the checkable option. More than 6 menu items are called expanded menu. Options menu is generated by onCreateOptionsMenu of the activity. This function is called only when the menu is generated for the first time. Any idea of changing the options menu can only be implemented in onPrepareOptionsMenu. This function will be called before the menu is displayed. OnOptionsItemSelected is used to process selected menu items.

Context menu is bound to a specific view. In activity, registerForContextMenu is used to register context menu for a view. Before the context menu is displayed, onCreateContextMenu is called to generate the menu. OnContextItemSelected is used to process selected menu items.

Android also provides the function of grouping menu items. You can divide menu items with similar functions into the same group. In this way, you can set menu properties by calling setGroupCheckable, setGroupEnabled, and setGroupVisible, you do not need to set it separately.

 

Generate dynamic Menu android. view. Menu. addIntentOptions (int groupId, int itemId, int order, ComponentName caller, Intent [] specifics, Intent intent, int flags, MenuItem [] outSpecificItems)

Specifics uses action + uri to add menu items for activating the corresponding activity.

Intent adds menu items to activate the corresponding activity in the general form of categroy + uri.

The difference between the Intent parameter and Specifics is that one uses categroy + uri to match the activity, and the other uses action + uri to match the activity.

// Search by Action

Intent [] specifics = new Intent [1];

Specifics [0] = new Intent (Intent. ACTION_EDIT, uri );

// Search by Category and set Action to null

Intent intent = new Intent (null, uri );

Intent. addCategory (Intent. CATEGORY_ALTERNATIVE );

MenuItem [] items = new MenuItem [1];

Menu. addIntentOptions (Menu. CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.