Android Learning Notes--menu Introduction (i) _android

Source: Internet
Author: User

Background:

Android3.0 (API level 11), the Android device no longer needs a special menu key.
With this change, Android apps should eliminate the reliance on traditional 6 menus. Instead, the Anction bar is provided to provide basic user functionality.

Create 3 Basic Menus
Options menu and Action Bar
The Options menu is a feature that has global impact in the app, such as "search", "Send Mail", "set", and so on.

In Android3.0 and above, the Options menu is placed on the action bar.

To create an option menu:

Overriding the Oncreateoptionmenu () method

@Override Public
 Boolean oncreateoptionsmenu (Menu menu) {
   Menuinflater inflater = Getmenuinflater ();
   Inflater.inflate (R.menu.game_menu, menu);
   return true;
}

There are two ways to add menu items: One is to refer to an XML file and the other is to use the Add () method.
In Android2.3, the system calls the Oncreateoptionmenu () method after the user clicks on the menu key, and at 3.0 and above, the system invokes the Oncreateoptionmenu () method at the beginning of the activity.

To handle a menu item response event:
Call onoptionsitemselected (MenuItem Item) method

@Override Public
Boolean onoptionsitemselected (MenuItem item) {
  //Handle Item selection
  switch ( Item.getitemid ()) {case
    r.id.new_game:
      newgame ();
      return true;
    Case R.ID.HELP:
      showHelp ();
      return true;
    Default: Return
      super.onoptionsitemselected (item);
  }

Dynamically changing the contents of the Options menu requires the use of the Onprepareoptionsmenu () method, which passes the menu object so that we can manipulate it, such as adding menu items, deleting the item, and so on.

Note that, at 3.0 and above, the Invalidateoptionsmenu () method needs to be invoked before using the Onprepareoptionsmenu method.

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.