(Android UI) Action Bar

Source: Internet
Author: User

The Action Bar indicates the user's current interface, adding multiple functional keys and a drop-down selection box to provide versatility.

Theme One: Let the app have Actionbar

Possible conditions one: support for Android 3.0 (API) and Above only

Step one: Specify the theme attribute value in the <Application> tag, android:theme= "@android: Style/theme.hole" to make the app Actionbar

<application    android:name= ". DemoApplication "    android:allowbackup=" true "    android:icon=" @drawable/ic_launcher "    android:theme=" @ Android:style/theme.holo ">    <activity        android:name=". Mainactivity "        android:label=" @string/app_name ">        <intent-filter>            <action android:name=" Android.intent.action.MAIN "/>            <category android:name=" Android.intent.category.LAUNCHER "/>        </intent-filter>    </activity>    <activity android:name= ". Secondactivity ">    </activity></application>

Possible condition two: Support Android 2.1 (API 7) and Above equivalent to the lower version of Android system

Step One: Use Actionbaractivity

public class Mainactivity extends Actionbaractivity {

Step Two: Use theme tags, specify specific topics, use attribute values: Android:theme= "@style/theme.appcompat.light"

<activity    android:theme= "@style/theme.appcompat.light"    android:name= ". Mainactivity "    android:label=" @string/app_name ">    <intent-filter>        <action android:name=" Android.intent.action.MAIN "/>        <category android:name=" Android.intent.category.LAUNCHER "/>    < /intent-filter></activity>

You can specify theme in the <application> or <activity> tab to achieve the above effect.

Topic Two: Adding buttons for Actionbar

The action Bar allows you to add buttons for the most important action items relating to the app's current context. Add the associated function keys for the current interface.

Step one: Design the. xml file for the Actionbar layout, create a new main_activity_actions.xml as its layout file in the Res/menu directory

The contents are as follows:

<menu xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:app= "http://schemas.android.com/ Apk/res-auto "    xmlns:tools=" Http://schemas.android.com/tools "    tools:context=" com.spt.MainActivity ">    <item        android:id= "@+id/action_search"        android:icon= "@drawable/ic_menu_search"        android: showasaction= "Ifroom"        android:title= "@string/action_search"/>    <item        android:id= "@+id/action_ Settings "        android:orderincategory=" "        android:title=" @string/action_settings "        android: Showasaction= "Never"/></menu>

Let the. xml file appear in the activity

    @Override Public    Boolean oncreateoptionsmenu (Menu menu) {        //inflate the menu items are in the action bar        Menuinflater inflater = Getmenuinflater ();        Inflater.inflate (r.menu.main_activity_actions, menu);        return Super.oncreateoptionsmenu (menu);    }

Overwrite Oncreateoptionmenu () and specify the. xml layout file in it.

The results appear as follows:

This declares the Search action should appear as a action button when the "is" available in the action Bar, but the S Ettings action should always appear in the overflow. (By default, all actions appear in the overflow, but it's good practice to explicitly declare your design intentions for E Ach action.)

The android:showasaction= "Ifroom" property in the <item> tab displays the icon as a button, whereas the assignment is "never", which is displayed in overflow.

Step Two: Add a response for the button

    @Override Public    Boolean onoptionsitemselected (MenuItem item) {        switch (Item.getitemid ()) {            case R.id.action_search:                openSearch ();                return true;            Case r.id.action_settings:                opensettings ();                return true;            Default:                return super.onoptionsitemselected (item);        }    }

Topic Three: Adding an up Button to an activity

As shown, the "<" arrow in the figure is actually a clickable button, and when clicked, returns to the specified activity.

Step One: Specify Parent for activity

<activity    android:name= ". Secondactivity "    android:label=" @string/title_activity_display_message "    android:parentactivityname=" Com.spt.MainActivity ">    <meta-data        android:name=" Android.support.PARENT_ACTIVITY "        android: Value= "Com.spt.MainActivity" >    </meta-data></activity>

Step Two: Set the following code in the show up Button:

Getactionbar (). Setdisplayhomeasupenabled (True);

The current activity already knows its parent, so there is no need to set a click event for it.

Theme four: Customizing Actionbar themes

Android includes a few built-in activity themes that include ' dark ' or ' light ' action bar styles. You can also extend these themes to further customize the look for your action bar.

1. The support Library APIs--the Theme.appcompat family of styles

2. API level One and higher--the Theme.holo family

Android:theme= "@android: Style/theme.holo.light.darkactionbar"

Android:theme= "@android: Style/theme.holo.light

When android:minsdkversion= "7", you can no longer use "@android: Style ..." and should use: Android:theme= "@style/theme.appcompat"

Android:theme= "@style/theme.appcompat.light"

Android:theme= "@style/theme.appcompat.light.darkactionbar"

Using a custom theme:

Android:theme= "@style/customactionbartheme"

Create a new Res/values/themes.xml file,

<?xml version= "1.0" encoding= "Utf-8"? ><resources xmlns:android= "http://schemas.android.com/apk/res/ Android >    <!--the theme applied to the application or activity--    <style name= " Customactionbartheme "parent=" @android: Style/theme.holo.light.darkactionbar ">        <item name=" Android: Actionbarstyle "> @style/myactionbar</item>    </style>    <style name=" Myactionbar "parent=" @ Android:style/widget.holo.light.actionbar.solid.inverse ">        <item name=" Android:background ">@ Drawable/actionbar_background</item>    </style></resources>

The above is the new style, named: Customactionbartheme

Where used to @drawable/actionbar_background, specify color; new drawable folder under Res directory

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >    <solid android:color= "#FFF4832C"/></shape>

For apps that use Android 2.1, you only need to replace the @android: Style/theme.holo.light.darkactionbar with @style/ Theme.AppCompat.Light.DarkActionBar @android: Style/widget.holo.light.actionbar.solid.inverse is replaced with: @style/ Widget.AppCompat.Light.ActionBar.Solid.Inverse

Theme four: Customizing font colors in Actionbar

<?xml version= "1.0" encoding= "Utf-8"? ><resources xmlns:android= "http://schemas.android.com/apk/res/ Android > <!--the theme applied to the application or activity--<style name= "Customactionbartheme" p        Arent= "@android: Style/theme.holo" > <item name= "Android:actionbarstyle" > @style/myactionbar</item> <item name= "Android:actionbartabtextstyle" > @style/myactionbartabtext</item> <item name= "Android: Actionmenutextcolor "> @color/actionbar_text</item> </style> <!--actionbar Styles and <sty Le name= "Myactionbar" parent= "@android: Style/widget.holo.actionbar" > <item name= "Android:titletextstyle" > @style/myactionbartitletext</item> </style> <!--ActionBar Title text-to-<style name= "myact Ionbartitletext "parent=" @android: Style/textappearance.holo.widget.actionbar.title "> <item name=" android: TextColor "> @color/actionbar_text</iteM> </style> <!--ActionBar tabs text styles-<style name= "Myactionbartabtext" parent= "@andro Id:style/widget.holo.actionbar.tabtext "> <item name=" Android:textcolor "> @color/actionbar_text</item&    Gt </style></resources>

Theme five: Cover Actionbar, achieve display and hide purpose

If, during the course of the user interaction, you want to hide and show the action Bar, you can do so by calling hide () and S How () on the ActionBar. However, this causes your activity to recompute and redraw the layout based on its new size.

(Android UI) Action Bar

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.