Android Menu, androidmenu

Source: Internet
Author: User

Android Menu, androidmenu

Android Menu creation requires the following packages:

Import android. view. Menu; import android. view. MenuItem; import android. widget. Toast; // The package name required by Toast.

PS: the simple method to search for registration is to hover your mouse over the data type of the required package, and then you will see the required package name in the eclipse prompt box.

You need to create a menu folder under your res folder (PS: I personally think that the name is only a code, as personal preferences, of course, if it is developed by a person) and then load an Android XML file under the folder

Then you can write items happily in it. The following is an example.

<? Xml version = "1.0" encoding = "UTF-8"?> <Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: id = "@ + id/one" android: title = "first_title_name"/> <item android: id = "@ + id/two" android: title = "second_title_name"/> // you can create many other options. </men>

Next you need to load your menu in your Activity

@ Override public boolean onCreatOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); // main is the name of the XML file you wrote in the menu (you can also choose another one ), in my understanding, the menu is to pass all the parameters into the menu object and return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {// This method of obtaining the id has to be spjected, and it seems very bad case R. id. one: Toast. makeText (this, "the words you want to say", Toast. LENGTH_SHORT ). show (); // In the written item, I choose to display a Toast break after I click it; case R. id. two: Toast. makeText (this, "the words you want to say", Toast. LENGTH_LONG ). show (); break; default:} return true ;}

 

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.