Android menu description 1: Overview

Source: Internet
Author: User

Menus are an important part of activity. It provides a great way for user operations. Android provides a simple framework to add standard menus to programs.

There are three types of menus:

Option menu

The main set of menu items of an activity. When you press the menu button (on the hardware), it will appear. If your program runs on Android 3.0 or later, you can directly put the menu item as "action items" on the actiion bar to provide quick selection.

Pop-up menu

A floating menu item is displayed when you hold down a view that has registered a pop-up menu.

Sub-menu

It is also a floating menu item that appears when you click a menu item that contains embedded menus.

This document describes how to create various types of menus, how to use XML to define menu content, and how to define callback functions in the activity to respond to menu items.

Create a menu resource

You need to define a menu in an XML menu resource instead of in the code, and then inflate the menu resource in the code. Using menu resources to define menus is a good practice, because this can separate the interface from the code. In addition, it is easier to design your menu in XML.

To create a menu resource, first create an XML file under Res/menu/of your project, and then create a menu with the following elements:

<Menu>

Define a menu, which is the container of the menu item. <Menu> must be the root node of the file. It can contain one or more <item> and <group> elements.

<Item>

Create a menu item. You can continue to include the <menu> element in the menu item. In this case, it has a sub menu.

<Group>

An optional, invisible container that contains <item> elements. It allows you to classify menu items so that similar menu items share some attributes, such as activity status and visible status. See the menu groups section.

The following is an example of a menu named game_menu.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: Id = "@ + ID/new_game" <br/> Android: icon = "@ drawable/ic_new_game" <br/> Android: Title = "@ string/new_game"/> <br/> <item Android: id = "@ + ID/help" <br/> Android: icon = "@ drawable/ic_help" <br/> Android: title = "@ string/help"/> <br/> </menu>In this example, two menu items are defined, each of which includes the following attributes:
Android: ID
A resource ID indicates a menu item. When you select a menu item, the program can use this ID to identify this menu item.
Android: icon
Reference A drawable icon for the menu item.
Android: Title
Reference a string used for the title of a menu item.

There are also many attributes that can be used in <item>, including the attributes that specify how menu items are displayed in action bar. For more information about XML syntax and menu resource attributes, see menu resource section.

Inflating a menu resource

In the code, use menuinflater. inflate () You can use inflate to convert an XML resource into an object in a program. for example, the following code uses the callback method oncreateoptionsmenu () to convert the game_menu.xml inflate file into a menu object and use it as the option menu of the activity:

@ Override <br/> Public Boolean oncreateoptionsmenu (menu) {<br/> menuinflater Inflater = getmenuinflater (); <br/> Inflater. inflate (R. menu. game_menu, menu); <br/> return true; <br/>}Getmenuinflater () returns a menuinflater of the activity. With this object, you can call inflate () to convert menu resources to menu objects. in this example, the menu resource is inflate to the parameter menu of the oncreateoptionsmenu () method. (This method will be discussed in detail in the next section ).

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.