Add ActionBar Buttons and androidactionbar to Android

Source: Internet
Author: User

Add ActionBar Buttons and androidactionbar to Android

1. Create an Xml file and label menu in the res/menu folder, and set item

<? Xml version = "1.0" encoding = "UTF-8"?> <Myapp: menu xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: myapp = "http://schemas.android.com/apk/res-auto"> <! -- Id: button Id --> <! -- Icon: button icon --> <! -- Title: button text --> <! -- ShowAsAction: button display status --> <item android: id = "@ + id/buttonIcon" android: icon = "@ drawable/ic_near_me_white" android: title = "navigation" myapp: showAsAction = "always"/> </myapp: menu>
View Code

2. Reference this menu. xml in Activity

  @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.buttons,menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case R.id.buttonIcon:                                 Toast.makeText(OrderDetailActivity.this,"hello world",Toast.LENGTH_SHORT).show();                        break;        }        return super.onOptionsItemSelected(item);    }
View Code

 

Android 3.0 or later

  • Always-> always display in Action bar;
  • IfRoom-> If the ActionBar has enough space, it is displayed;
  • Never-> not displayed in Action Bar, collapsed in Over Flow;
  • WithText-> the menu item is displayed with its icon and menu text)

 

Android 2.0 or later

Namespace needs to be introduced

  • Add a custom namespace to menu. xml

Xmlns: myapp = "http://schemas.android.com/apk/res-auto"

  • Specify the namespace before ShowAsAction
     myapp:showAsAction="always"

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.