"Android Document" Training-------Adding the Action Bar

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/chziroy/article/details/44171197


This article mainly translates several articles about Actionbar in the Android development website training

Setting up the action bar in most of the most basic formats, the action bar renders the activity's title and the icon that renders the app on the left. It allows the user to know which activity is currently in, and makes the app have a persistent flag. Such as

To use the Action Bar requires the app's activity to use the theme with the Action Bar (theme), theme is related to the minimum version supported by your app. This article only speaks of android3.0 above (API >= 11). (The original is to explain how to adapt to the 3.0 below, I think: now 2.3, 2.2 version of the equipment is really too little too little, when necessary to do the appropriate for them, learning to study directly over 3.0)
Support for Android 3.0 and Above only
Starting with Android3.0 (API level 11), all activity includes the action bar by default, and the action Bar uses the Theme.holo theme or its subtopics. So if you want to use Action bar in all your activity, just set the app's Targetsdkversion and minsdkversion to 11 and above, as follows
<manifest >    <uses-sdk android:minsdkversion= "One" .../>    ...</manifest>

Note that if you customize a theme, the custom theme needs to inherit from Theme.holo. Well, with the above settings, the Theme.holo theme is set to all the activity in your entire app.
Adding Action Buttons
Action Bar can add actions related to the current context button. The more important buttons can be rendered directly on the action bar, and the less important buttons are placed in the overflow, such as
Specify the Actions in XML
To add a button to the action bar, you can create a new XML file in the menu folder that defines the <item&gt, as follows
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <!--Search, should appear as action Button--    <item android:id= "@+id/action_search"          android:icon= "@drawable/ic_action_search"          android:title= "@string/action_search"          android:showasaction= "Ifroom"/>    <!--Settings, should always Be in the overflow-    <item android:id= "@+id/action_settings"          android:title= "@string/action_settings "          android:showasaction=" Never "/></menu>
Here, when the action bar control is sufficient, the Search action button is rendered directly on the action bar, and if the setting action is always hidden in overflow. By default all actions are in overflow, but the official recommendation is to declare the location for each item yourself.
Add the Actions to the Action Bar
How do I add the above declared menu items to the action bar? You can implement the activity's Oncreateoptionmenu () to inflate a menu resource file into the method's menu Parameter object. As follows:
@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {    //inflate the menu items for use in the action bar    Menui Nflater inflater = Getmenuinflater ();    Inflater.inflate (r.menu.main_activity_actions, menu);    return Super.oncreateoptionsmenu (menu);}

Respond to Action Buttons
In response to the events of the individual buttons in the action bar, simply rewrite the activity's onoptionsitemselected () method, as follows
@Overridepublic boolean onoptionsitemselected (MenuItem item) {    //Handle presses on the action bar items    switch (i Tem.getitemid ()) {case        R.id.action_search:            openSearch ();            return true;        Case r.id.action_settings:            opensettings ();            return true;        Default:            return super.onoptionsitemselected (item);}    }

Add up Button for low-level activities all activity that is not an app entry needs to provide an up-return function. Such as


At the beginning of Android 4.1 (API level 16), you only need to declare a parent activity for activity in manifest. As follows
<application ... > ...    <!--the main/home activity (it has no parent activity)--    <activity        android:name= " Com.example.myfirstapp.MainActivity "...> ...    </activity>    <!--A Child of the main activity--    <activity        android:name= " Com.example.myfirstapp.DisplayMessageActivity "        android:label=" @string/title_activity_display_message "        android:parentactivityname= "Com.example.myfirstapp.MainActivity" >        <!--Parent activity Meta-data To support 4.0 and lower-        <meta-data            android:name= "Android.support.PARENT_ACTIVITY"            Android : value= "com.example.myfirstapp.MainActivity"/>    </activity></application>

If you are in version 4.1 and above, you only need to use Parentactivityname, the above meta-data is for version 4.0 and below. But in order to be compatible, in fact, you can always write two of them together. Then call setdisplayhomeasupenabled () in the Activity code: Make the app's icon the button to return up, as follows:
@Overridepublic void OnCreate (Bundle savedinstancestate) {    super.oncreate (savedinstancestate);    Setcontentview (r.layout.activity_displaymessage);    Getsupportactionbar (). Setdisplayhomeasupenabled (true);    If your minsdkversion is one or higher, instead use:    //Getactionbar (). Setdisplayhomeasupenabled (True);

Styling the Action Bar
Android offers some built-in action bar themes, and you can personalize your own action bar theme based on these themes
Use of an Android Theme
Android offers two basic themes
    • Theme.holo: A brown theme
    • Theme.light: a white theme
You can set the theme to all activity in <application>, or assign a theme to an activity in <activity>. As follows:
<application android:theme= "@android: Style/theme.holo.light" .../>

You can also use the black head, white theme theme: Theme.Holo.Light.DarkActionBar






Customize the Background
In general, you can customize the style of the action bar by overriding the background of the theme as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources>    <!--the theme applied to the application or Activity--    <style name= "Customactionbartheme"           parent= "@style/theme.holo.light.darkactionbar" >        <item name= "Android:actionbarstyle" > @style/myactionbar</item>    </style>    <!-- ActionBar Styles--    <style name= "Myactionbar"           parent= "@style/ Widget.Holo.Light.ActionBar.Solid.Inverse ">        <item name=" Android:background "> @drawable/actionbar_ Background</item>    </style></resources>

Then apply the theme to the entire app:
<application android:theme= "@style/customactionbartheme" .../>

Using the Action Bar of navigation tabs or split action Bar, you can also specify theme styles using backgroundstacked and Backgroundsplit.
Customize the Text color is similar to modifying the background, modifying the font color also requires modifying the corresponding attribute, and you can generally modify the following three action Bar title: Using titletextstyleaction bar tabs: Using Actionbartabtextstyle
Action buttons: Using Actionmenutextcolor
as follows: Res/values/themes.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <!--the theme applied to the application or activity --<style name= "Customactionbartheme" parent= "@style/theme.holo" > <item name= "android:act Ionbarstyle "> @style/myactionbar</item> <item name=" Android:actionbartabtextstyle "> @style/ myactionbartabtext</item> <item name= "Android:actionmenutextcolor" > @color/actionbar_text</item > </style> <!--ActionBar Styles---<style name= "Myactionbar" parent= "@style/widget. Holo.actionbar "> <item name=" Android:titletextstyle "> @style/myactionbartitletext</item> </styl E> <!--ActionBar title text--<style name= "Myactionbartitletext" parent= "@style/textappeara nCE. Holo.Widget.ActionBar.Title "> <item name=" Android:textcolor "> @color/actionbar_text</item> </st yle> <!--ActionBar tabs TexT styles---<style name= "Myactionbartabtext" parent= "@style/widget.holo.actionbar.tabtext" > <item name= "Android:textcolor" > @color/actionbar_text</item> </style></resources>

Customize the Tab Indicator
The main is to talk about the press and release of the background changes, easy to understand, but the expression of lengthy, not to write

Overlaying the Action Bar
The Action bar defaults to the top of the activity view, which in some way narrows the size of the activity's layout. You can hide or show the action bar using Actionbar's Hide () or show () during user interaction. However, it causes the system to recalculate the size of the activity and redraw it. To prevent Actionbar from changing the size of the activity when calling hide () or show (), you can set the action Bar to overlay mode, where action bar does not occupy the activity space. It is at the top of the activity and may obscure part of the activity. However, this does not change the size of the activity when it calls hide () or show (). (You can set the action bar to be transparent if you are avoiding some of the activity's content in overlay mode.)
Enable Overlay Mode
Using the overlay mode, just use the Android:windowactionbaroverlay property and set it to true. The following (all the code explained in this article is for android3.0 or more)
<resources>    <!--the theme applied to the application or activity--    <style name= " Customactionbartheme "           parent=" @android: Style/theme.holo ">        <item name=" Android: Windowactionbaroverlay ">true</item>    </style></resources>

Specify Layout Top-margin If you want to not let Actionbar cover activity in overlay mode, you can set the activity's margintop or paddingtop to action Bar's height. As follows
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:paddingtop="? android:attr/actionbarsize ">    ...</relativelayout>


Translate an article first, and then translate another article in the website API guide



















"Android Document" Training-------Adding the Action Bar

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.