Android Menu, androidmenu

Source: Internet
Author: User

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 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.