How to create Actionbar Android

Source: Internet
Author: User

See https://developer.android.com/training/basics/actionbar/adding-buttons.html

1. Set the Actionbar item in the XML file
    • The root node of the file is menu and placed under the Res/menu folder
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item         android:id= "@+id/action_search"        android:icon= "@drawable/ic_action_ Search "        android:title=" Find "        android:showasaction=" Ifroom "/>    <item         android:id= "@+id/action_setting"        android:title= "Settings"        android:showasaction = "Never"/></menu>
2. Display Actionbar
    • Actionbar has been added by default in later versions of android3.0, we only need to display the actions defined above
    • The inflater here is dedicated to the Menuinflater.

    @Override    publicboolean  oncreateoptionsmenu (Menu menu) {          this. Getmenuinflater ();        Inflater.inflate (r.menu.actionbar_actions, menu);         return Super . Oncreateoptionsmenu (menu);    }
3. Add a key response
@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.action_search:toast.maketext ( This, "Looking for ... ", Toast.length_short). Show ();  Break;  CaseR.id.action_overflow:toast.maketext ( This, "More Options ... ", Toast.length_short). Show (); }        return Super. onoptionsitemselected (item); }
4. Add return main activity function
    • You need to indicate in the configuration list that the parent activity:parent Activity does not need to change
    • You need to call the method to display the up key (back to main activity) in Actionbar
    • We do not need to define the up key handling method
        <activity             android:name= ". Actionbaractivity "            android:parentactivityname=" Com.njulya.demo_view. Mainactivity ">            <meta-data                android:name=" Android.support.PARENT_ACTIVITY "                 Android:value= "Com.njulya.demo_view. Mainactivity "/>        </activity>

    @Override    publicboolean  oncreateoptionsmenu (Menu menu) {          this. Getmenuinflater ();        Inflater.inflate (r.menu.actionbar_actions, menu);         // Add return main activity function        Getactionbar (). setdisplayhomeasupenabled (true);         return Super . Oncreateoptionsmenu (menu);    }

5. Add a common Searchview control
    • You need to set the Actionviewclass property
    • See http://developer.android.com/guide/topics/ui/actionbar.html

    <item         android:id= "@+id/action_search"        android:icon= "@drawable/ic_action_search"         Android:title= "Find"        android:showasaction= "Ifroom|collapseactionview"         Android:actionviewclass= "Android.widget.SearchView"/>

How to create Actionbar Android

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.