Package Com.example.menu2;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;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;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.id.create: Toast.maketext (mainactivity.this, "Create File", Toast.length_short). Show (); break;case r.id.open:intent Intent = new Intent ( Mainactivity.this, Nextactivity.class); item.setintent (intent);//toast.maketext (Mainactivity.this, "Open File", Toast.length_short). Show (); Break;case R.id.load:toast.maketext (Mainactivity.this, "load", Toast.length_short). Show ( ); Break;case R.id.save:toast.maketext (Mainactivity.this, "save", Toast.length_short). Show (); break;default:break;} return super.onoptionsitemselected (item);}}
The main layout file can be used without the control
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " ></RelativeLayout>
Res/menu/main.xml Defining menus
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <!--orderincategory Menu Order category--<!- -app:showasaction= "Never" means that it will not appear on the title bar--<item android:id= "@+id/menu_settings" android:icon= "@dra Wable/communication "android:orderincategory=" android:showasaction= "Ifroom|withtext" Android:tit Le= "Menu Settings" > <menu> <item android:id= "@+id/create" Android:title = "New File" > </item> <item android:id= "@+id/open" android:titl e= "Open File" > </item> </menu> </item> <item android:id= "@+id/system" android:icon= "@drawable/gear" android:orderincategory= "101" android:showasaction= "Ifroom|withtext" android:title= "User Menu" > <menu> <group android:id= "@+id/group1" > <item AndroidOid:id= "@+id/load" android:title= "Load" > </item> </group> <group android:id= "@+id/group2" > <item android:id= "@+id/save" android:title= "Save" > </item> </group> </menu> </item> ;</menu>
Define a page for a menu jump
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent "> <textview android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:layout_alignparenttop=" true " android:layout_centerhorizontal=" true " Android: layout_margintop= "172DP" android:textsize= "30sp" android:text= "This is the page to jump"/></relativelayout>
Package Com.example.menu2;import Android.app.activity;import Android.os.bundle;public class NextActivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_next);}}
Showasaction Specify where the menu is displayed this is never.
If you change to ifroom|withtext, it means that if actionbar has space, it appears in Actionbar and displays the title
Source code Download
This project in the simulator debugging normal, on the real machine debug level Two menu cannot open, do not know whether the cell phone problem
Menu (iii) Level two menu and title bar menu