Loading Menus Through XML Files

来源:互联网 发布:英语数字读法规则 知乎 编辑:程序博客网 时间:2024/06/03 14:30

this Article quoted  from Pro Android 4    page 216

Copyright © 2012 by Satya Komatineni and Dave MacLean


Up until this point, we’ve created all our menus programmatically. This is not the most 
convenient way to create menus, because for every menu, you have to provide several 
IDs and define constants for each of those  IDs. No doubt this is tedious.  
Instead, you can define menus through XML files, which is possible in Android because 
menus are also resources. The XML approach to menu creation offers several 
advantages, such as the ability to name menus,  order them automatically, and give them 
IDs. You can also get localization support for the menu text.  
Follow these steps to work with XML-based menus: 
1.   Define an XML file with menu tags. 
2.   Place the file in the  /res/menu  subdirectory. The name of the file is arbitrary, and 
you can have as many files as you want. Android automatically generates a 
resource ID for this menu file. 
3.   Use the resource ID for the menu file to  load the XML file  into the menu. 
4.   Respond to the menu items using the resource IDs generated for each menu item. 
The following sections talk about each of these steps and provide corresponding code 
snippets. 
Structure of an XML Menu Resource File 
First, let’s look at an XML file with menu definitions (see Listing 7–14). All menu files start 
with the same high-level menu tag followed by a series of  group  tags. Each of these  group  
tags corresponds to the menu  item group you talked about  at the beginning of the 
chapter. You can specify an ID for the group using the  @+id approach. Each menu group 
has a series of menu items with their menu item IDs tied to symbolic names. You can 
refer to the Android SDK documentation for all the possible arguments for these XML 
tags. 
Listing 7–14.  An XML File with Menu Definitions 
<menu xmlns:android="http://schemas.android.com/apk/res/android">     <!-- This group uses the default category. -->     <group android:id="@+id/menuGroup_Main">          <item android:id="@+id/menu_testPick"             android:orderInCategory="5"             android:title="Test Pick" />         <item android:id="@+id/menu_testGetContent"             android:orderInCategory="5"             android:title="Test Get Content" />         <item android:id="@+id/menu_clear"             android:orderInCategory="10"             android:title="clear" />         <item android:id="@+id/menu_dial"             android:orderInCategory="7"             android:title="dial" />         <item android:id="@+id/menu_test"             android:orderInCategory="4"             android:title="@+string/test" />         <item android:id="@+id/menu_show_browser"             android:orderInCategory="5"             android:title="show browser" />     </group> </menu>

The menu XML file in Listing 7–14 has one group. Based on the resource ID definition 
@+id/menuGroup_main , this group is automatically  assigned a resource ID called 
menuGroup_main in the R.java resource ID file. Similarly, all the child menu items are 
allocated menu item IDs based on their symbolic resource ID de finitions in this XML file. 
Inflating XML Menu Resource Files 
Let’s assume that the name of this XML file is  my_menu.xml . You need to place this file in 
the  /res/menu  subdirectory. Placing the file in /res/menu  automatically generates a 
resource ID called  R.menu.my_menu. 
Now, let’s look at how you can use this menu  resource ID to popula te the options menu. 
Android provides a class called android.view.MenuInflater  to populate  Menu objects 
from XML files. You use an instance of this  MenuInflater to make use of the 
R.menu.my_menu resource ID to populate a menu object. This is shown in Listing 7–15. 
Listing 7–15.  Using the Menu Inflater 
@Override public boolean onCreateOptionsMenu(Menu menu)  {    MenuInflater inflater = getMenuInflater(); //from activity    inflater.inflate(R.menu.my_menu, menu);     //It is important to return true to see the menu    return true;     } 

In this code, you first get the MenuInflater from the  Activity class and then tell it to 
inflate the menu XML file  into the menu directly.  
Responding to XML-Based Menu Items 
You respond to XML menu items the way you respond to menus created 
programmatically, but with a small difference. As before,  you handle the menu items in 
the  onOptionsItemSelected()  callback method. This time, you have some help from 
Android’s resources (see Chapter 3 for details on resources). As mentioned in the 
section “Structure of an XM L Menu Resource File,” Android not only generates a 
resource ID for the XML file  but also generates the necessary menu item IDs to help you 
distinguish between the menu items. This is an advantage in terms of responding to the 
menu items because you don’t have to explicitly create and manage their menu item 
IDs.  
NOTE:  The resource type to identify  the ID of a menu item ( R.id.some_menu_item_id) is 
different from the resource type  to identify the menu itself ( R.menu.some_menu_file_id). 

To further elaborate on this, in the case of XML menus, you don’t have to define 
constants for these IDs and you don’t have to worry about their uniqueness because 
resource ID generation takes care of that . The code in Listing 7–16 illustrates. 

Listing 7–16.  Responding to Menu Items from an XML Menu Resource File 
private void onOptionsItemSelected (MenuItem item) {    if (item.getItemId() ==  R.id.menu_clear )    {         //do something    }    else if (item.getItemId() == R.id.menu_dial)    {         //do something    }     ......etc  } 

Notice how the menu item names from the XML menu resource file have automatically 
generated menu item IDs in the R.id space. 
Starting in SDK 3.0, you can use the  android:onClick attribute of a menu item to 
directly indicate the name of a method in an activity that is attached to this menu. This 
activity method is then called with the menu item object as the sole  input. This feature is 
only available in 3.0 and above. Listing 7–17 shows an example. 
Listing 7–17.  Specifying a Menu Callback Method in an XML Menu Resource File 
<item android:id="... "         android:onClick="a-method-name-in-your-activity"        ... </item>

更多中文资料:

博客园:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html