In the upper right corner of most Android programs, a menu button, such as the plus sign in the upper right corner of the interface, is set.
This requires a new folder named menu in the layout sibling directory, and then right click on the new menu new XML file:
XML File Code:
<menu xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/ Apk/res-auto "> <item android:id=" @+id/id_action_add " app:showasaction=" Always " Android: Title= "Add" android:icon= "@mipmap/ic_launcher" android:orderincategory= "1" /> <item android:id= "@+id/id_action_delete" app:showasaction= "always" android:title= "Jian" Android : icon= "@mipmap/ic_launcher" android:orderincategory= "1" /></menu>
The top two item represents a few buttons on the top of your menu bar. Android:icon You can customize a picture of a menu item
Then there's the mainactivity. To rewrite the code:
@Override Public Boolean oncreateoptionsmenu (Menu menu) { //new XML file getmenuinflater (). Inflate ( R.menu.main, menu); return Super.oncreateoptionsmenu (menu); } @Override Public Boolean onoptionsitemselected (MenuItem Item) { //click on different buttons depending on the ID to control what the activity needs to do switch (Item.getitemid ()) { case r.id. Id_action_add: //Event break ; Case R.id. Id_action_delete: //Event break ; } return true; }
Memo .....
Android studio--Creating menu menu items