1、Creating MenusCreating MenusMenus are an important part of an application that provide a familiar interface for the user to access application functions and settings. Android offers an easy programming interface for you to provide application menus in your application.Android provides three types of
2、 application menus:Options MenuThe primary menu for an Activity, which appears when the user presses the device MENU key. Within the Options Menu are two groups: Icon MenuThe menu items visible at the bottom of the screen at the press of the MENU key. It supports a maximum of six menu items. These a
3、re the only menu items that support icons and the only menu items that do not support checkboxes or radio buttons.Expanded MenuThe vertical list of menu items exposed by the More menu item in the Icon Menu. When the Icon Menu is full, the expanded menu is comprised of the sixth menu item and the res
4、t.Context MenuA floating list of menu items that appears when the user performs a long-press on a View. SubmenuA floating list of menu items that the user opens by pressing a menu item in the Options Menu or a context menu. A submenu item cannot support a nested submenu. Defining MenusInstead of ins
5、tantiating Menu objects in your application code, you should define a menu and all its items in an XML menu resource, then inflate the menu resource (load it as a programmable object) in your application code. Defining your menus in XML is a good practice because it separates your interface design f
6、rom your application code (the same as when you define your Activity layout).To define a menu, create an XML file inside your projects res/menu/ directory and build the menu with the following elements:Creates a Menu, which is a container for menu items. It must be the root node and holds one or mor
7、e of the following elements. You can also nest this element in an to create a submenu.Creates a MenuItem, which represents a single item in a menu.An optional, invisible container for elements. It allows you to categorize menu items so they share properties such as active state and visibility. See M
8、enu groups.For example, here is a file in res/menu/ named game_menu.xml:menu xmlns:android= This example defines a menu with two menu items. Each item includes the attributes:android:idA resource ID thats unique to the item so that the application can recognize the item when the user selects it.andr
9、oid:iconA drawable resource that is the icon visible to the user.android:titleA string resource that is the title visible to the user.For more about the XML syntax and attributes for a menu resource, see the Menu Resource reference.Inflating a Menu ResourceYou can inflate your menu resource (convert
10、 the XML resource into a programmable object) using MenuInflater.inflate(). For example, the following code inflates the game_menu.xml file defined above during the onCreateOptionsMenu() callback method, to be used for the Options Menu:Overridepublic boolean onCreateOptionsMenu(Menu menu) MenuInflat
11、er inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true;The getMenuInflater() method returns a MenuInflater for the Activity. With this object, you can call inflate(), which inflates a menu resource into a Menu object. In this example, the menu resource defined by game
12、_menu.xml is inflated into the Menu that was passed into onCreateOptionsMenu(). (This callback method for creating an option menu is discussed more in the next section.)Creating an Options MenuFigure 1. Screenshot of an Options Menu.The Options Menu is where you should include basic application func
13、tions and necessary navigation items (for example, a button to open application settings). The user can open the Options Menu with the device MENU key. Figure 1 shows a screenshot of an Options Menu.When opened, the first visible portion of the Options Menu is called the Icon Menu. It holds the firs
14、t six menu items. If you add more than six items to the Options Menu, Android places the sixth item and those after it into the Expanded Menu, which the user can open with the More menu item.When the user opens the Options Menu for the first time, Android calls your Activitys onCreateOptionsMenu() m
15、ethod. Override this method in your Activity and populate the Menu that is passed into the method. Populate the Menu by inflating a menu resource as described in Inflating a Menu Resource. (You can also populate the menu in code, using add() to add menu items.)When the user selects a menu item from
16、the Options Menu, the system calls your Activitys onOptionsItemSelected() method. This method passes the MenuItem that the user selected. You can identify the menu item by calling getItemId(), which returns the unique ID for the menu item (defined by the android:id attribute in the menu resource or
17、with an integer passed to the add() method). You can match this ID against known menu items and perform the appropriate action.For example:Overridepublic boolean onOptionsItemSelected(MenuItem item) / Handle item selection switch (item.getItemId() case R.id.new_game: newGame(); return true; case R.i
18、d.quit: quit(); return true; default: return super.onOptionsItemSelected(item); In this example, getItemId() queries the ID for the selected menu item and the switch statement compares the ID against the resource IDs that were assigned to menu items in the XML resource. When a switch case successful
19、ly handles the item, it returns true to indicate that the item selection was handled. Otherwise, the default statement passes the menu item to the super class in case it can handle the item selected. (If youve directly extended the Activity class, then the super class returns false, but its a good p
20、ractice to pass unhandled menu items to the super class instead of directly returning false.)Tip: If your application contains multiple activities and some of them provide the same Options Menu, consider creating an Activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemS
21、elected() methods. Then extend this class for each Activity that should share the same Options Menu. This way, you have to manage only one set of code for handling menu actions and each decendent class inherits the menu behaviors.If you want to add menu items to one of your decendent activities, ove
22、rride onCreateOptionsMenu() in that Activity. Call super.onCreateOptionsMenu(menu) so the original menu items are created, then add new menu items with menu.add(). You can also override the super classs behavior for individual menu items.Changing the menu when it opensThe onCreateOptionsMenu() metho
23、d is called only the first time the Options Menu is opened. The system keeps and re-uses the Menu you define in this method until your Activity is destroyed. If you want to change the Options Menu each time it opens, you must override the onPrepareOptionsMenu() method. This passes you the Menu objec
24、t as it currently exists. This is useful if youd like to remove, add, disable, or enable menu items depending on the current state of your application.Note: You should never change items in the Options Menu based on the View currently in focus. When in touch mode (when the user is not using a trackb
25、all or d-pad), Views cannot take focus, so you should never use focus as the basis for modifying items in the Options Menu. If you want to provide menu items that are context-sensitive to a View, use a Context Menu.Creating a Context MenuA context menu is conceptually similar to the menu displayed w
26、hen the user performs a right-click on a PC. You should use a context menu to provide the user access to actions that pertain to a specific item in the user interface. On Android, a context menu is displayed when the user performs a long press (press and hold) on an item.You can create a context men
27、u for any View, though context menus are most often used for items in a ListView. When the user performs a long-press on an item in a ListView and the list is registered to provide a context menu, the list item signals to the user that a context menu is available by animating its background colorit
28、transitions from orange to white before opening the context menu. (The Contacts application demonstrates this feature.)Register a ListViewIf your Activity uses a ListView and you want all list items to provide a context menu, register all items for a context menu by passing the ListView to registerF
29、orContextMenu(). For example, if youre using a ListActivity, register all list items like this:registerForContextMenu(getListView();In order for a View to provide a context menu, you must register the view for a context menu. Call registerForContextMenu() and pass it the View you want to give a cont
30、ext menu. When this View then receives a long-press, it displays a context menu.To define the context menus appearance and behavior, override your Activitys context menu callback methods, onCreateContextMenu() and onContextItemSelected().For example, heres an onCreateContextMenu() that uses the cont
31、ext_menu.xml menu resource:Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu);MenuInflater is used to inflate the context menu f
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1