Android Development notes (9) -- initially set Menu and androidmenu

Source: Internet
Author: User

Android Development notes (9) -- initially set Menu and androidmenu
Reprinted Please note: http://www.cnblogs.com/igoslly/p/6858656.htmlSet Menu Set Menu and add button operation on ActionBar:Add the menu folder under the main/res directory and create the main. xml file.

    <menu  xmlns:android="http://schemas.android.com/apk/res/android"                xmlns:app=" http://schemas.android.com/apk/res/tool">            <item                     android:id ="@+id/coffee_cart"                     android:icon="@drawble/cart"                     android:title="Delete"                     app:showAsAction=”always”/>    </menu>

ShowAsActionThe attribute has five values: ifRoom, never, always, withText, and collapseActionView.

IfRoom is displayed in Item, and the never will never be displayed based on the width of the screen. Only the title is displayed in the overflow list, so it is best to include the title when defining the item. Always are always displayed no matter whether they are overflow or not. The withText value displays the text title. If the icon is valid and restricted by space, the text title may not be displayed completely. The collapseActionView is folded into a button. The operation window is displayed when the user selects it. Generally, it works with ifRoom.

  In MainActivity, You need to rewrite the onCreateOptionsMenu function. 
    @Override        public boolean onCreateOptionsMenu(Menu menu) {            // Inflate the menu; this adds items to the action bar if it is present.            getMenuInflater().inflate(R.menu.activity_main, menu);            return true;        }

 

Set the Button FunctionItem. getItemId () to obtain the ID of the click button. Because each item has a unique ID, use the switch-case-default statement to determine and execute the command
    @Override        public boolean onOptionsItemSelected(MenuItem item) {            switch(item.getItemId()){               case R.id. coffee_cart:                               Toast.makeText(this, "CoffeeCart", Toast.LENGTH_SHORT).show(); }            }
Statement:

1. The text and pictures in this note are both original and original. For reprinted documents, enter "blog garden-igoslly ".

2, Android development courses in 2017 4 years to participate in Google developer learning, notes original http://www.studyjamscn.com/thread-20580-1-1.html#pid272486


 

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.