(1) Main.xml file in the Res--menu directory
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/file" android:orderincategory= "android:showasaction=" "Always" android:title= "file (F)" > <menu> <item android:id= "@+id/create" android:title= "new" > </item> <item android:id= "@+id/open" android:title= "open" > </item> </menu> </item> <item android:id= "@+id/edit" android:orderincategory= "100" Android:showasaction= "Always" android:title= "edit (E)" > <menu> <item Android Oid:id= "@+id/create1" android:title= "new" > </item> <item an Droid:id= "@+id/open1" android:title= "open" > </item> </menu> </item> <item AndroidOid:id= "@+id/format" android:orderincategory= "android:showasaction=" Always "android:title=" format (O) "> <menu> <item android:id=" @+id/create2 "android:title=" new "&G T </item> <item android:id= "@+id/open2" android:title= "open" > </item> </menu> </item></menu>
(2) File of class
Package Com.example.menu_showasaction;import Android.os.bundle;import Android.app.activity;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 onmenuitemselected (int featureid, MenuItem item) {switch (Item.getitemid ()) {case R.id.create: Toast.maketext (Mainactivity.this, "Create ...", toast.length_short); break;default:break;} Return super.onmenuitemselected (Featureid, item);}}
Settings for the Showasaction property of the menu of the Android learning note