Adding menu menus to Android is simple.
Steps:
1. Add the items you want to add in the Main.xml file in the menu folder.
<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Tools:context= "Com.example.menu.MainActivity" > <!--It's easy to understand what this file does, which is to add entries to the menu. An item represents an entry - <ItemAndroid:id= "@+id/fist"Android:title= "First Menu" ></Item> <ItemAndroid:id= "@+id/second"Android:title= "Second Menu" ></Item> <ItemAndroid:id= "@+id/third"Android:title= "Third Menu" ></Item></Menu>
2. In the activtiy file, rewrite the Oncreateoptionsmenu (Menu menu) method while writing the listening content in onoptionsitemselected (MenuItem item)
PackageCom.example.menu;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//This indicates that the menu file is loaded, and the first parameter indicates that the menu is created from that resource file.//The second indicates that the menu is passed into that object. Here we use menu to pass in menu//The general system helps us create a goodgetmenuinflater (). Inflate (R.menu.main, menu); return true; } //How to listen to menus@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.fist:toast.maketext ( This, "clicked on the first menu", 0). Show (); Break; CaseR.id.second:toast.maketext ( This, "clicked on the second menu", 0). Show (); Break; CaseR.id.third:toast.maketext ( This, "clicked on the third menu", 0). Show (); Break; default: Break; } return true; }}
It's OK. Just modify these two files, others do not have to modify.
Let's see how it works.
Android Add menu