Android Actionbar Usage Explanation

Source: Internet
Author: User

I. Introduction of Actionbar

Actionbar is located at the top of the activity and is used to display the activity's icon, title, and menu. can be used for navigation and other functions, widely used in view of the interaction. Actionbar can be divided into three parts, namely the first icon, the second item, the Third overflow button.

second, how to add Actionbar. (ActionBar is added to SK in Android 3.0 (API 11) and wants to use the support Library V7 with ActionBar in a lower version.) )

1, the new activity inherits from Appcompatactivty.

public class Mainactivity extends Appcompatactivity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }}
2. Implement Theme.AppCompat.Light.DarkActionBar Theme

<application        android:allowbackup= "true"        android:icon= "@mipmap/ic_launcher"        android:label= "@ String/app_name "        android:supportsrtl=" true "        android:theme=" @style/apptheme ">        <activity Android:name= ". Mainactivity ">            <intent-filter>                <action android:name=" Android.intent.action.MAIN "/>                <category android:name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </activity >    </application>
Style file theme configuration.

<resources>    <!--Base Application Theme--    <style name= "Apptheme" parent= " Theme.AppCompat.Light.DarkActionBar >        <!--Customize your Theme here--        <item name= " Colorprimary "> @color/colorprimary</item>        <item name=" Colorprimarydark "> @color/ colorprimarydark</item>        <item name= "coloraccent" > @color/coloraccent</item>    </style ></resources>
3, the following for you to figure out the theme theme profiles in the style are what, predecessors summarized.

4, the final following

Third, remove the Actionbar.

1. Modify Theme Theme

<style name= "Apptheme" parent= "Theme.AppCompat.Light.NoActionBar" >        <!--Customize your Theme here.-- >        <item name= "colorprimary" > #55A028 </item>        <item name= "Colorprimarydark" > @color/ colorprimarydark</item>        <item name= "coloraccent" > @color/coloraccent</item>    </style >
2, modify the code.

Package Com.example.cdy.actionbardemo;import Android.support.v7.app.actionbar;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;public class Mainactivity extends appcompatactivity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate ( Savedinstancestate);        Setcontentview (r.layout.activity_main);        Get ActionBar, note my is V7 pack, use Getsupportactionbar ()        ActionBar ActionBar = Getsupportactionbar ();        Hide ActionBar        actionbar.hide ();    }}
Iv. Modifying the style of the Actionbar

1, modify the Actionbar icon.

The title of Actionbar, by default, inherits the Lable attribute of applicition and activity, so you only need to modify the activity's Lable property. It is worth mentioning that the old version of the change icon is also the default inherited from the Logo property, but found that the previous period of time after the update can not be displayed, the code needs to be set.

<activity android:name= ". Mainactivity "            android:label=" @string/actionbar_title ">            <intent-filter>                <action android: Name= "Android.intent.action.MAIN"/>                <category android:name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>
public class Mainactivity extends Appcompatactivity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Get ActionBar, note my is V7 pack, use Getsupportactionbar ()        ActionBar ActionBar = Getsupportactionbar ();        When using the V7 package, it is necessary to specify a property to display the icon and caption.        actionbar.setdisplayshowhomeenabled (true);        Actionbar.setlogo (r.mipmap.back_black);        Actionbar.setdisplayuselogoenabled (true);        Setcontentview (R.layout.activity_main);    }
2.

v. Add Item and Overfloy.

1. Define all action item in the menu's resource file. Showasaction (How this item is displayed on Actionbar) icon (icon) title (title). Showasaction property, never does not appear on the Actionbar, ifroom if there is room to display, no space is displayed on the overflow, always only displayed on the Actionbar.

<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android"    xmlns:app= "Http://schemas.android.com/apk/res-auto" >    <item        android:id= "@+id/action_delete"        android:orderincategory= "app:showasaction="        never "        android:icon=" @mipmap/ic_launcher "        android:title= "Delete"/>    <item        android:id= "@+id/action_add"        android:orderincategory=        " app:showasaction= "Ifroom"        android:icon= "@mipmap/ic_launcher"        android:title= "add"/>    <item        android:id= "@+id/action_select"        android:orderincategory= "        app:showasaction="        "Always" android:icon= "@mipmap/ic_launcher"        android:title= "select"/></menu>
2. Reference the resource file in the activity.

&NBSP;&NBSP;

Package Com.example.cdy.actionbardemo;import Android.support.v7.app.actionbar;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuinflater;import Android.view.menuitem;import Android.widget.toast;public class MainActivity Extends Appcompatactivity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (sav        Edinstancestate);        Get ActionBar, note my is V7 pack, use Getsupportactionbar () ActionBar ActionBar = Getsupportactionbar ();        When using the V7 package, it is necessary to specify a property to display the icon and caption.        Actionbar.setdisplayshowhomeenabled (TRUE);        Actionbar.setlogo (R.mipmap.back_black);        Actionbar.setdisplayuselogoenabled (TRUE);    Setcontentview (R.layout.activity_main);         }/** * @param menu * @return * Use menus */@Override public boolean Oncreateoptionsmenu (Menu menu) {        Menuinflater inflater = Getmenuinflater ();    Inflater.inflate (R.menu.actionbar_menu, menu);    return Super.oncreateoptionsmenu (menu); }/** * @param item * @return * Item's Click event */@Override public boolean onoptionsitemselected (Menui TEM item) {switch (Item.getitemid ()) {//action with ID Action_refresh is selected case R.                Id.action_delete:Toast.makeText (This, "delete", Toast.length_short). Show ();            Break Action with ID action_settings is selected case R.id.action_add:toast.maketext (this, "add",                Toast.length_short). Show ();            Break                Case R.id.action_select:toast.maketext (This, "select", Toast.length_short). Show ();            Break        Default:break;    } return super.onoptionsitemselected (item); }}
3. Effect


VI. Use a custom view to add to the Actionbar.

1. Custom Layout_actionbar.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:gravity=" Center_vertica         L "android:paddingleft=" 10DP "android:paddingright=" 10DP "> <imagebutton android:id=" @+id/left_imbt " Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentl Eft= "true" android:layout_centervertical= "true" android:background= "@mipmap/back_black"/> < TextView android:id= "@+id/bar_title" android:layout_width= "wrap_content" android:layout_height= "Wrap_        Content "Android:layout_centerinparent=" true "android:textcolor=" #FFFFFFFF "android:textsize=" 18SP " android:text= "title"/> <!--actionbar Right button-<textview android:id= "@+id/right_bt" an Droid:layout_width= "Wrap_content"        android:layout_height= "Wrap_content" android:layout_alignparentright= "true" Android:layout_centerver Tical= "true" android:textsize= "18sp" android:background= "@null"/></relativelayout>
2. Reference in activity and get control

Package Com.example.cdy.actionbardemo;import Android.graphics.color;import Android.support.v7.app.ActionBar; Import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import android.view.LayoutInflater; Import Android.view.menu;import android.view.menuinflater;import android.view.menuitem;import Android.view.View; Import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Appcompatactivity {//Statement control    Pieces TextView Bar_title;    TextView RIGHT_BT;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Get ActionBar, note my is V7 pack, use Getsupportactionbar () ActionBar ActionBar = Getsupportactionbar ();        When using the V7 package, it is necessary to specify a property to display the icon and caption.        Actionbar.setdisplayshowhomeenabled (TRUE);        Actionbar.setlogo (R.mipmap.back_black);        Actionbar.setdisplayuselogoenabled (TRUE);        Returns the arrow (not shown by default) actionbar.setdisplayhomeasupenabled (false); Left icon Click event Enable aCtionbar.sethomebuttonenabled (TRUE);        Displays the custom ActionBar actionbar.setdisplayshowcustomenabled (true);        View actionbarlayout = Layoutinflater.from (this). Inflate (R.layout.layout_actionbar, NULL);        Actionbar.setcustomview (actionbarlayout);        Bar_title = (TextView) Actionbarlayout.findviewbyid (r.id.bar_title);        RIGHT_BT = (TextView) Actionbarlayout.findviewbyid (R.ID.RIGHT_BT);        Bar_title.settext ("my Task");        Right_bt.settext ("filter");        Bar_title.settextcolor (Color.White);        Right_bt.settextcolor (Color.White);    Setcontentview (R.layout.activity_main);         }/** * @param menu * @return * Use menus */@Override public boolean Oncreateoptionsmenu (Menu menu) {    return Super.oncreateoptionsmenu (menu); }/** * @param item * @return * Item's Click event */@Override public boolean onoptionsitemselected (Menui    TEM item) {return super.onoptionsitemselected (item); }}
3. Effect

ActivityBar The use of the general introduction to this, ActivityBar there are many ways to use, and special effects need us beforehand. We can do a good study. Welcome to correct and reprint

Android Actionbar Usage Explanation

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.