(1) Layout file not changed
(2) The code in the Main.xml file in the Res--menu directory is as follows:
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <!--Level menu--<item Android : id= "@+id/file" android:title= "@string/file" > <!--Level Two menu--<menu> <item Android:id= "@+id/create" android:title= "@string/create" > </item> <item android:id= "@+id/open" android:title= "@string/open" > </ITEM&G T </menu> </item> <!---level menu-<item android:id= "@+id/chioce" android:title= "@s Tring/chioce "> <!--Level Two menu--<menu> <item android:id=" @+id/creat E1 "android:title=" @string/create "> </item> <item android: Id= "@+id/open1" android:title= "@string/open" > </item> </menu> </item ></menu&Gt
(3) in the String.xml file under the values directory:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >menu_submenu</ string> <string name= "action_settings" >Settings</string> <string name= "Hello_world" >hello world!</string> <string name= "file" > File </string> <string name= "Create" > New </string> <string name= "open" > Open </string> <string name= "Chioce" > Options </string ></resources>
(3) The Mainactivity.java code is as follows:
Package Com.example.menu_submenu;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.view.menu;import Android.view.menuitem;import Android.view.SubMenu;import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} /* * Processing level Menu */@Overridepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.id.file: Toast.maketext (Mainactivity.this, "level menu file:", Toast.length_short). Show (); Break;case R.id.chioce:toast.maketext ( Mainactivity.this, "level menu Chioce ...", Toast.length_short). Show (); break;default:break;} return super.onoptionsitemselected (item);} /* * Processing Level two menu */@Overridepublic Boolean OnmenuitemseleCTED (int featureid, MenuItem item) {switch (Item.getitemid ()) {case R.id.create1:toast.maketext (mainactivity.this, " Process level Two menu create1: ", Toast.length_short). Show (); Break;case R.id.open1:toast.maketext (Mainactivity.this," Process level Two menu create1: ", Toast.length_short). Show (); Break;case R.id.create:toast.maketext (Mainactivity.this," Process level Two menu create: ", Toast.length_short). Show (); Break;case R.id.open:toast.maketext (mainactivity.this," Processing level two menu open ... " , Toast.length_short). Show (); break;default:break;} Return super.onmenuitemselected (Featureid, item);}}
Use of the menu level menu, level two menus for Android learning notes