Android Design Toolbar

Source: Internet
Author: User

Design Toolbar Action Bar (custom toolbar type)

Toolbars provide a familiar and predictable way for users to perform some kind of action and manipulate the application, but that doesn't mean it needs to look the same as other applications. If you want to design a toolbar that makes it more suitable for your product's logo, using Android's style and theme resources can be a little easier.

Android contains some built-in activity themes (build-in activity themes) such as "dark" or "light" toolbar styles. Users can also extend these topics to further customize the appearance of their own toolbars.

Note: If you use support library APIs to design toolbars, you must overload the Theme.appcompat function family instead of the Theme.holo function family. In doing so, each user declares a style feature that must be declared two times: once using the style features of the platform (the Android:properties), a single-use style feature is included in the support library, such as the AppCompat. R.attr Properties-The content of these properties is the real application.

Using Android Themes

Android contains two basic themes to specify the color of the toolbar:

Theme.holo specifying a "dark" theme

Theme.Holo.Light specifying a "light" theme

By declaring these topics into the program's Menifest file, the user can apply these topics to the entire application or to a separate activity (individual activity), declaring it android:theme and placing it in < The application> element or the <activity> element. For example:

<android:theme= "@android: Style/theme.holo.light"/>

Users can also declare Theme.Holo.Light.DarkActionBar to use a black toolbar while other activities use light color

When using a support library, you must use the Theme.appcompat theme

Theme.appcompat Black Theme

Theme.AppCompat.Light Light Theme

Theme.AppCompat.Light.DarkActionBar a light theme with a black toolbar

Make sure the toolbar icon matches the color of the toolbar. In addition, the Action bar icon Pack contains a standard action icon that can be used, both a full-color and full-black toolbar.

Custom background

To change the toolbar background, you need to create a user theme for the user's activity activities, overloading the Actionbarstyle property. This attribute points to another style, in which the user can reload the Background property to define a drawable resource for the toolbar background.

If your program uses navigation tabs or split action Bar, you can use the backgroundstacked and Backgroundsplit properties to bars clear backgrounds for each of these columns.

Note: It is important to declare an appropriate parent topic for your user topic. Without a parent topic, your toolbars might lose a lot of style properties unless you explicitly declare them.

For Android3.0 or higher versions

You can define the toolbar background as follows

Res/values/themes.xml
<?
XML version= "1.0" encoding= "Utf-8"?><Resources> <!--The theme applied to the application or activity - <stylename= "Customactionbartheme"Parent= "@android: Style/theme.holo.light.darkactionbar">
<Itemname= "Android:actionbarstyle">@style/myactionbar</Item></style><!--ActionBar Styles - <stylename= "Myactionbar"Parent= "@android: Style/widget.holo.light.actionbar.solid.inverse"> <Itemname= "Android:background">@drawable/actionbar_background</Item> </style></Resources>
Custom text color

Changing the color of text in the toolbar requires that you overload different properties for each text element.

    • Toolbar title: Create user style to explicitly textcolor properties and explicit TitleTextStyle properties, in program Actionbarstyle

Note: The user style applied to TitleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style

    • Toolbar tabs: Overloading Actionbartabtextstyle in the user's active theme
    • Action button: Reload Actionmenutextcolor function in user's active theme (activity theme)
Res/values/themes.xml
<?
XML version= "1.0" encoding= "Utf-8"?><Resources> <!--The theme applied to the application or activity - <stylename= "Customactionbartheme"Parent= "@style/theme.holo"> <Itemname= "Android:actionbarstyle">@style/myactionbar</Item>
<Itemname= "Android:actionbartabtextstyle">@style/myactionbartabtext</Item> <Itemname= "Android:actionmenutextcolor">@color/actionbar_text</Item> </style> <!--ActionBar Styles - <stylename= "Myactionbar"Parent= "@android: Style/widget.holo.actionbar"> <Itemname= "Android:titletextstyle">@style/myactionbartitletext</Item> </style> <!--ActionBar Title Text - <stylename= "Myactionbartitletext"Parent= "@android: Style/textappearance.holo.widget.actionbar.title">
<Itemname= "Android:textcolor">@color/actionbar_text</Item>
</style> <!--ActionBar Tabs Text Styles - <stylename= "Myactionbartabtext"Parent= "@style/widget.holo.actionbar.tabtext"> <Itemname= "Android:textcolor">@color/actionbar_text</Item>
</style></Resources>
Custom-made tabulation indicator (tabs indicator)

To change the indicator indicator of the navigation label navigation tabs, you need to create an active theme Overload Actionbarstyle property. This property points to another style resource, in which the user needs to reload the background property, and this property must be explicitly state-list drawable.

Note: State-list drawable is important so that the currently selected label indicates that its background status is different from other tags. For more information about how to create a drawable resource that can handle multiple button states, consult the State List document.

For example, here is a state-list drawable declares a clear background picture, with several different states of the toolbar tabs,

Res/drawable/actionbar_tab_indicator.xml<?XML version= "1.0" encoding= "Utf-8"?>
<selectorxmlns:android= "Http://schemas.android.com/apk/res/android">
<!--states when BUTTON was not PRESSED - <!--Non focused states -
<Itemandroid:state_focused= "false"android:state_selected= "false"android:state_pressed= "false"android:drawable= "@drawable/tab_unselected" />
<Itemandroid:state_focused= "false"android:state_selected= "true"android:state_pressed= "false"android:drawable= "@drawable/tab_selected" />
<!--Focused states (such as when Focused with a d-pad or mouse hover) -
<Itemandroid:state_focused= "true"android:state_selected= "false"android:state_pressed= "false"android:drawable= "@drawable/tab_unselected_focused" />
<Itemandroid:state_focused= "true"android:state_selected= "true"android:state_pressed= "false"android:drawable= "@drawable/tab_selected_focused" />
<!--states when BUTTON is PRESSED -
<!--Non focused states -
<Itemandroid:state_focused= "false"android:state_selected= "false"android:state_pressed= "true"android:drawable= "@drawable/tab_unselected_pressed" />
<Itemandroid:state_focused= "false"android:state_selected= "true"android:state_pressed= "true"android:drawable= "@drawable/tab_selected_pressed" />
<!--Focused states (such as when Focused with a d-pad or mouse hover) -
<Itemandroid:state_focused= "true"android:state_selected= "false"android:state_pressed= "true"android:drawable= "@drawable/tab_unselected_pressed" />
<Itemandroid:state_focused= "true"android:state_selected= "true"android:state_pressed= "true"android:drawable= "@drawable/tab_selected_pressed" />
</
selector>
Res/values/themes.xml
<?
XML version= "1.0" encoding= "Utf-8"?><Resources> <!--The theme applied to the application or activity - <stylename= "Customactionbartheme"Parent= "@style/theme.holo"> <Itemname= "Android:actionbartabstyle">@style/myactionbartabs</Item> </style> <!--ActionBar Tabs Styles - <stylename= "Myactionbartabs"Parent= "@style/widget.holo.actionbar.tabview"> <!--Tab Indicator - <Itemname= "Android:background">@drawable/actionbar_tab_indicator</Item> </style></Resources>

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.