Android Learning Note--menu (i)

Source: Internet
Author: User

    • Background:

Starting with Android3.0 (API level 11), Android devices no longer require a dedicated menu key.

With this change, Android apps should remove their reliance on traditional 6-item 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 is placed in the app that has global impact, such as "search", "Send Mail", "settings", etc.

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

      • To create a menu of options:
      • Overriding the Oncreateoptionmenu () method
      • 1 @Override 2  Public Boolean Oncreateoptionsmenu (Menu menu) {3     Menuinflater inflater = getmenuinflater (); 4     inflater.inflate (r.menu.game_menu, menu); 5     return true ; 6 }

        There are two ways to add menu items: One is to reference an XML file, and the other is to use the Add () method.

Android2.3, the system calls the Oncreateoptionmenu () method after the user taps the menu key, and at 3.0 and above, the system calls the Oncreateoptionmenu () method when the activity is started.

Handling menu Item Response events:

Call the onoptionsitemselected(MenuItem Item) method

1 @Override2  Public Booleanonoptionsitemselected (MenuItem item) {3     //Handle Item Selection4     Switch(Item.getitemid ()) {5          CaseR.id.new_game:6 newgame ();7             return true;8          CaseR.id.help:9 showHelp ();Ten             return true; One         default: A             return Super. onoptionsitemselected (item); -     } -}

Changing the contents of the Options menu dynamically requires a method that passes the menu onPrepareOptionsMenu() object so that we can manipulate it, such as adding menu items, deleting menus, and so on. Note that at 3.0 and above, you need to call the invalidateoptionsmenu () method before using the Onprepareoptionsmenu method.

Android Learning Note--menu (i)

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.