1. first create a new folder menu (right-res directory ---> New ----> Directory) Enter a folder named menu in the res directory, click OK
2. Create a file called main menu (right menu folder ----> New ------> Menu resource file) enter the file name and click OK in the main menu under the new directory
Which reads:
<item android:id="@+id/add_item"
android :title="添加"/>
<item android:id="@+id/remove_item"
android: title = "remove" />
3. rewriting the onCreateOptionsMenu MainActivity () method, as follows:
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater () inflate (R.menu.main, menu);. // The first parameter specifies to create a menu by the resource file, the second parameter specifies our menu items will be added where a menu object.
return true;
}
4. rewriting onOptionsItemSelected in MainActivity () method
public boolean onOptionItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.add_item:
Toast.makeText (this, "you click on the add!", Toast.LENGTH_SHORT) .show ();
break;
case R.id.remove_item:
Toast.makeText (this, "you click to remove!", Toast.LEMGTH_SHORT) .show ();
break;
default:
}
return true;
}
Run a simple menu can be found on the realization of
}