Directly in the code to add menu items, to the menu item grouping, etc., this is a more traditional approach, it has some shortcomings.
For example, in response to each menu item, we need to use constants to save the ID of each menu item, and so on.
For this reason, Android provides a better way to define menu as the resource of the application, and the local support of the resources through Android, which makes it easier for us to create and respond to menus. B
This article describes how to use XML files to load and respond to menus, and we need to do these steps:
1. Create a menu folder under the/res directory
2. Using the menu-related element definition XML file in the menu directory, the file name is arbitrary, and Android automatically generates a resource ID for it.
For example: R.menu.menu the Menu.xml resource file corresponding to the menu directory
3. Use the resource ID of the XML file to add the menu items defined in the XML file to the Menus object
4. When responding to menu items, use the resource ID corresponding to each item.
Package Cn.fansunion.menu;import Android.app.activity;import Android.os.bundle;import android.view.Menu;import Android.view.menuinflater;import Android.view.menuitem;import Android.widget.toast;import Cn.fansunion.R;public Class Optionmenuwithxmlactivity extends Activity {public void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.option_menu);} public boolean Oncreateoptionsmenu (Menu menu) {menuinflater inflater = Getmenuinflater (); Inflater.inflate (R.menu.menu, menu); return true;} public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {Case R.id.menu_one:toast.maketext ( This, "You clicked Menu_one", Toast.length_long). Show (); Break;case R.id.submenu_one:toast.maketext (This, "you clicked Submenu_one" , Toast.length_long). Show (); Break;default:return super.onoptionsitemselected (item);} return true;}}
Rec/menu Directory
Menu.xml
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu_one" android:orderincategory= "3" android:title= "Android app development really interesting "/> <item android:id=" @+id/menu_two "android:orderincategory=" 2 "android:title=" iOS development is also good "/&G T <item android:id= "@+id/menu_three" android:enabled= "false" android:orderincategory= "4" Android Oid:title= "Java server is also good"/> <!--Part 2: The settings of the submenu, will nest a <menu> inside the MenuItem, and in this example submenu, experiment with the shortcut keys--<it EM android:id= "@+id/submenu" android:orderincategory= "3" android:title= "ray interest" ><menu > <item android:id= "@+id/submenu_one" android:alphabeticshortcut= "n" a ndroid:title= "Internet Technology" android:visible= "true"/> <item android:id= "@+id/submen U_two "Android: alphabeticshortcut= "G" android:title= "investment Banking" android:visible= "true"/> </menu> ;</item></menu>
For more information on defining menu through XML, refer to the following 3 articles, copy a bunch of others, everyone else's API and attribute usage, it's not interesting.
Resources:
Android uses XML to define Menu
http://blog.csdn.net/jaycee110905/article/details/8765082
Customizing menu resources in Android
http://ybxu-123.blog.163.com/blog/static/594737702010680575834/
Android Menu Details (v)--Using the XML Build menu
Http://www.cnblogs.com/codingmyworld/archive/2011/08/28/2155594.html
Android uses XML to define Menu