Android Studio -- create Menu items, androidmenu

Source: Internet
Author: User

Android Studio -- create Menu items, androidmenu

In most android apps, a menu button is set in the upper right corner of the interface, for example, the plus sign in the upper right corner.

In this case, you need to create a folder named menu in the directory of the same level as layout, and right-click the newly created menu to create an xml file:

Xml file code:

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">    <item        android:id="@+id/id_action_add"        app:showAsAction="always"        android:title="add"        android:icon="@mipmap/ic_launcher"        android:orderInCategory="1"        />    <item        android:id="@+id/id_action_delete"        app:showAsAction="always"        android:title="jian"        android:icon="@mipmap/ic_launcher"        android:orderInCategory="1"        /></menu>

The two items above represent the buttons on your menu bar. Android: Images of menu items that can be customized in the icon

The following is the MainActivity code to be rewritten:

 

@ Override public boolean onCreateOptionsMenu (Menu menu) {// the newly created xml file getMenuInflater (). inflate (R. menu. main, menu); return super. onCreateOptionsMenu (menu) ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// click a button Based on different IDs to control the event switch (item. getItemId () {case R. id. id_action_add: // event break; case R. id. id_action_delete: // event break;} return true ;}

 

Memo .....

 

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.