Android Menu, androidmenu
For example, in the upper-right corner, the first method of code is as follows:
Package com. example. menu; 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 {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // used to create a Menu. When the interface is opened, the public boolean onCreateOptionsMenu (menu Menu) {// getMenuInflater menu filling // getMenuInflater () is automatically called (). inflate (R. menu. main, menu); // add parameter (group id, current option id, sorting, title) menu. add (, 1, "personal Center"); return true;} // menu option event public boolean onOptionsItemSelected (MenuItem item) {int id = item. getItemId (); if (id = 12) {Toast. makeText (this, "success", Toast. LENGTH_SHORT ). show ();} return super. onOptionsItemSelected (item );}}
The second method is to add a configuration file in the root directory "res/menu/main. xml:
<Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: id = "@ + id/action_settings" android: orderInCategory = "100" android: showAsAction = "never" android: title = "personal Center" android: icon = "@ android: drawable/ic_menu_help"/> </menu>
The second method is used in the source code, which is a little different from the first method:
Public boolean onCreateOptionsMenu (Menu menu) {// The getMenuInflater menu is filled with getMenuInflater (). inflate (R. menu. main, Menu); return true ;}