Android Add menu

Source: Internet
Author: User

Adding menu menus to Android is simple.

Steps:

1. Add the items you want to add in the Main.xml file in the menu folder.

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Tools:context= "Com.example.menu.MainActivity" >    <!--It's easy to understand what this file does, which is to add entries to the menu. An item represents an entry -    <ItemAndroid:id= "@+id/fist"Android:title= "First Menu"        ></Item>          <ItemAndroid:id= "@+id/second"Android:title= "Second Menu"        ></Item>              <ItemAndroid:id= "@+id/third"Android:title= "Third Menu"        ></Item></Menu>

2. In the activtiy file, rewrite the Oncreateoptionsmenu (Menu menu) method while writing the listening content in onoptionsitemselected (MenuItem item)

 PackageCom.example.menu;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//This indicates that the menu file is loaded, and the first parameter indicates that the menu is created from that resource file.//The second indicates that the menu is passed into that object. Here we use menu to pass in menu//The general system helps us create a goodgetmenuinflater (). Inflate (R.menu.main, menu); return true; }    //How to listen to menus@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.fist:toast.maketext ( This, "clicked on the first menu", 0). Show ();  Break;  CaseR.id.second:toast.maketext ( This, "clicked on the second menu", 0). Show ();  Break;  CaseR.id.third:toast.maketext ( This, "clicked on the third menu", 0). Show ();  Break; default:             Break; }        return true; }}

It's OK. Just modify these two files, others do not have to modify.

Let's see how it works.

Android Add menu

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.