Android series UI components-Menu

Source: Internet
Author: User

Android series UI components-Menu
[Body] According to the official documentation, Android devices no longer require a special menu button starting with Android3.0 (API level 11). Instead, ActionBar is recommended. Therefore, many new devices on the market now use three virtual buttons, and no additional menu buttons are provided. Due to the development of the Android version, menu support varies greatly. Android3.0 is a watershed and can be divided into the following three types: OptionMenu and ActionBar: a set of operations, if the development platform is above Android3.0, ActionBar is recommended. If the development platform is under Android2.3 or, OptionMenu can still be used. ContextMenu and ActionMode: ContextMenu is a floating window to display a list of options. ActionMode is an operation bar displayed on the top of the screen. You can select multiple options, actionMode is supported only after Android3.0. Popup Menu: PopupMenu is a modal Menu fixed on the View. It is displayed in the pop-up mode and is supported only after Android3.0. [Define a menu in XML] Android provides a standard resource file in XML format to define menu items and supports all menu types. We recommend that you use an XML resource file to define menus, then Inflater it into Activity or Fragment, instead of using code declaration in Activity. The XML resource file of the menu must be created in the/res/menu/directory and contain the following elements: <Menu>: Define a menu, is the root node of a menu resource file. It can contain one or more <item> and <group> elements. <Item>: Creates a MenuItem, which represents an option in the menu. <Group>: You can group menu items and operate them in groups. <Item> in addition to regular support for the id, icon, and title attributes, an element also has an important attribute: android: showAsAction, which is compatible, this section describes how to add menu items to ActionBar in Android later versions. <Group> is used to group menus. The menu display effect after grouping is no different. The only difference is that you can operate on the menu group. In this way, for menu items of classification, it is easier to operate and provides the following operations: Menu. setGroupCheckable (): whether all menus in the menu group are optional. Menu. setGroupVisible (): whether to hide all menus in the Menu group. Menu. setGroupEnabled (): whether the Menu of the Menu group is useful. If the menu item needs to be selected by one or more options, you can use the android: checkableBehavior attribute to set a group for a single <item> or <group>. This attribute accepts three parameters: single, single choice; all, multiple choices, none, no Checked option, default. After creating an XML menu resource file, you can use the MenuInflater. inflate () method to fill in the menu resource, so that the XML resource becomes a programmable object. I. Options menu option MENU: OptionMenu, option menu. Click the MENU on the mobile phone. The device must have a menu button before it can be triggered. Because of screen restrictions, you can only display up to six menu items. If more than six menu items are defined, other menu items will be hidden, the sixth Menu displays "more". Click to expand more menus. Although the option menu is no longer recommended after android, if it is used, the option menu items will be transferred to the ActionBar by default on devices after android. This can be implemented through android: showAsAction property control. Core steps for creating the option Menu: (1) rewrite the onCreateOptionMenu (menu Menu) method of the Activity. When the menu is loaded for the first time, call (2) Call add () of the Menu () you can also call the setIcon () method of MenuItem to set the icon for the menu item. (Note: After Android 3.0, the icon is not displayed even if it is added.) (3) override the OptionsItemSelected (MenuItem item) of the Activity to respond to the Click Event of the menu item (MenuItem) to see the specific code implementation: Create the Android project MenuTest: method 1: Add Menu option (1) IN res/menu/main. define menu items in xml. Main. the xml Code is as follows: copy the Code <menu xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" tools: context = "com. example. menutest. mainActivity "> <item android: id =" @ + id/start "android: orderInCategory =" 100 "android: showAsAction =" never "android: title = "@ string/start"/> <item android: id = "@ + id/over" android: orderInCategory = "200" android: showAsAction = "never" android: t Itle = "@ string/over"/> </menu> copy the code. Note: string references of lines 9th and 15th must be set in the strings. xml file in advance. (2) MainActivity. java: Copy code 1 package com. example. menutest; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. view. menu; 6 import android. view. menuItem; 7 import android. widget. toast; 8 9 10 public class MainActivity extends Activity {11 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity_main); 16} 17 18 19 // rewrite the onCreateOptionMenu (Menu menu) method. When the Menu is loaded for the first time, call 20 @ Override21 public boolean onCreateOptionsMenu (menu Menu) {22 // Inflate the menu; this adds items to the action bar if it is present.23 // fill in the option Menu (read, parse, and load the XML file to the menu component) 24 getMenuInflater (). inflate (R. menu. main, menu); 25 return true; 26} 27 28 // rewrite OptionsItemSelected (MenuItem item) to respond to the Click Event of the menu item (MenuItem) (which item is determined by id) 29 @ Override30 public boolean onOptionsItemSelected (MenuItem item) {31 // Handle action bar item clicks here. the action bar will32 // automatically handle clicks on the Home/Up button, so long33 // as you specify a parent activity in AndroidManifest. xml.34 switch (item. getItemId () {35 case R. id. start: 36 Toast. makeText (this, "Start game", Toast. LENGTH_SHORT ). show (); 37 break; 38 case R. id. over: 39 Toast. makeText (this, "end game", Toast. LENGTH_SHORT ). show (); 40 break; 41 42 default: 43 break; 44} 45 return super. onOptionsItemSelected (item); 46} 47}

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.