Android官方入门文档[7]样式化操作栏

Android官方入门文档[7]样式化操作栏

 

Styling the Action Bar
样式化操作栏

 

This lesson teaches you to
1.Use an Android Theme
2.Customize the Background
3.Customize the Text Color
4.Customize the Tab Indicator

You should also read
•Styles and Themes
•Android Action Bar Style Generator

这节课教你
1.使用Android的主题
2.自定义背景
3.自定义文本颜色
4.自定义选项卡指示器

你也应该阅读
•样式和主题
•Android的操作栏样式生成器

The action bar provides your users a familiar and predictable way to perform actions and navigate your app, but that doesn't mean it needs to look exactly the same as it does in other apps. If you want to style the action bar to better fit your product brand, you can easily do so using Android's style and theme resources.
操作栏中提供了用户熟悉的和可预测的方式来进行操作和浏览您的应用程序,但是,这并不意味着它必须看起来完全因为它在其他应用程序一样。如果你想样式化操作栏,以更好地满足您的产品商标,你可以很容易做到使用Android的风格和主题资源。

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.
Android包括一些内置活动主题,包括“黑暗dark”或“轻light”操作栏的样式。您还可以扩展这些主题进一步定制的外观为您操作栏。

Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform's style properties (the android: properties) and once using the style properties included in the Support Library (the appcompat.R.attr properties—the context for these properties is actually your app). See the examples below for details.
注:如果您使用的是支持库的API操作栏中,则必须使用(或重写)的Theme.AppCompat家族的风格(而不是Theme.Holo家庭,提供API级别11或更高)。这样做,你声明每个样式属性必须被声明两次:一次使用平台的样式属性(是Android:属性),一旦使用的样式属性包含在支持库(该appcompat.R.attr属性 - 的上下文这些特性实际上是你的应用程序)。详情参见下面的例子。

 

Use an Android Theme
使用Android的主题

--------------------------------------------------------------------------------

Android includes two baseline activity themes that dictate the color for the action bar:
•Theme.Holo for a "dark" theme.
•Theme.Holo.Light for a "light" theme.
Android的包括两个基本的活动主题,决定了操作栏的颜色:
•Theme.Holo一个“黑暗drak”的主题。
•Theme.Holo.Light一个“光light”的主题。

 

You can apply these themes to your entire app or to individual activities by declaring them in your manifest file with the android:theme attribute for the <application> element or individual <activity> elements.
您可以通过声明他们在您的manifest清单文件与为<application>元素或独立的<activity>元素的android:theme属性来适用于您的整个应用程序或单独活动。

For example:
例如:

<application android:theme="@android:style/Theme.Holo.Light" ... />

You can also use a dark action bar while the rest of the activity uses the light color scheme by declaring the Theme.Holo.Light.DarkActionBar theme.
您也可以使用深色操作栏,而活动的其余部分通过声明Theme.Holo.Light.DarkActionBar主题使用光的配色方案。

When using the Support Library, you must instead use the Theme.AppCompat themes:
•Theme.AppCompat for the "dark" theme.
•Theme.AppCompat.Light for the "light" theme.
•Theme.AppCompat.Light.DarkActionBar for the light theme with a dark action bar.
当使用支持库,则必须改用Theme.AppCompat主题:
•Theme.AppCompat为“黑暗dark”的主题。
•Theme.AppCompat.Light为“光light”的主题。
•Theme.AppCompat.Light.DarkActionBar为主题的光light与暗的操作栏。

Be sure that you use action bar icons that properly contrast with the color of your action bar. To help you, the Action Bar Icon Pack includes standard action icons for use with both the Holo light and Holo dark action bar.
请确保您使用的操作栏图标,适当的和你的操作栏的颜色对比。为了帮助您的操作栏图标包包括标准动作图标同时与Holo light光与黑暗drak 操作栏使用。

 

Customize the Background
自定义背景

--------------------------------------------------------------------------------


To change the action bar background, create a custom theme for your activity that overrides the actionBarStyle property. This property points to another style in which you can override the background property to specify a drawable resource for the action bar background.
要改变操作栏背景,创建自定义主题为您的活动,覆盖actionBarStyle属性。此属性指向另一种风格中,你可以重写background属性来指定动作栏背景绘制的资源。

If your app uses navigation tabs or the split action bar, then you can also specify the background for these bars using the backgroundStacked and backgroundSplit properties, respectively.
如果你的应用程序使用的导航选项卡或拆分操作栏,那么你也可以指定背景这些酒吧使用backgroundStacked和backgroundSplit属性,分别。

Caution: It's important that you declare an appropriate parent theme from which your custom theme and style inherit their styles. Without a parent style, your action bar will be without many style properties unless you explicitly declare them yourself.
注意:这是你声明的合适的父主题,从您的自定义主题和风格继承他们的风格是很重要的。如果没有父母样式化,你的操作栏将不很多样式属性,除非你明确自己声明它们。

 

For Android 3.0 and higher only
针对Android3.0和更高版本

 

When supporting Android 3.0 and higher only, you can define the action bar's background like this:
当支持Android的3.0和更高版本,可以定义操作栏的背景是这样的:

res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 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>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>
</resources>

Then apply your theme to your entire app or individual activities:
那么你的主题应用到整个应用程序或个人活动:

<application android:theme="@style/CustomActionBarTheme" ... />

 

For Android 2.1 and higher
针对Android2.1及更高

 

When using the Support Library, the same theme as above must instead look like this:
当使用支持库,与上面相同的主题,而必须是这样的:

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.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_background</item>
    </style>
</resources>

Then apply your theme to your entire app or individual activities:
那么你的主题应用到整个应用程序或个人活动:

<application android:theme="@style/CustomActionBarTheme" ... />

 

Customize the Text Color
自定义文本颜色

--------------------------------------------------------------------------------

To modify the color of text in the action bar, you need to override separate properties for each text element:
要修改操作栏中文本的颜色,你需要重写每一个文本元素单独的属性:

•Action bar title: Create a custom style that specifies the textColor property and specify that style for the titleTextStyle property in your custom actionBarStyle.
•操作栏标题:创建一个自定义样式,指定textColor属性,并指定风格的自定义actionBarStyle的titleTextStyle属性。

Note: The custom style applied to titleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style.
注:适用于titleTextStyle自定义风格应该使用TextAppearance.Holo.Widget.ActionBar.Title父风格。

•Action bar tabs: Override actionBarTabTextStyle in your activity theme.
•操作栏选项卡:在您的活动主题覆盖actionBarTabTextStyle。

•Action buttons: Override actionMenuTextColor in your activity theme.
•动作按钮:在您的活动主题覆盖actionMenuTextColor。

 

For Android 3.0 and higher only
针对Android3.0和更高版本

 

When supporting Android 3.0 and higher only, your style XML file might look like this:
当支持Android的3.0和更高版本,你的风格XML文件可能是这样的:

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:actionBarStyle">@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>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText"
           parent="@style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>

    <!-- ActionBar tabs text styles -->
    <style name="MyActionBarTabText"
           parent="@style/Widget.Holo.ActionBar.TabText">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>
</resources>

 

For Android 2.1 and higher
针对Android2.1及更高

 

When using the Support Library, your style XML file might look like this:
当使用支持库,你的风格XML文件可能是这样的:

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.AppCompat">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="android:actionMenuTextColor">@color/actionbar_text</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="actionMenuTextColor">@color/actionbar_text</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

        <!-- Support library compatibility -->
        <item name="titleTextStyle">@style/MyActionBarTitleText</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText"
           parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>

    <!-- ActionBar tabs text -->
    <style name="MyActionBarTabText"
           parent="@style/Widget.AppCompat.ActionBar.TabText">
        <item name="android:textColor">@color/actionbar_text</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>
</resources>

 

Customize the Tab Indicator
自定义选项卡指示器

--------------------------------------------------------------------------------

To change the indicator used for the navigation tabs, create an activity theme that overrides the actionBarTabStyle property. This property points to another style resource in which you override the background property that should specify a state-list drawable.
要更改用于导航标签的指示,创建一个活动主题,覆盖actionBarTabStyle属性。此属性指向在其中覆盖应该指定一个状态的列表绘制背景属性另一种风格的资源。

Note: A state-list drawable is important so that the tab currently selected indicates its state with a background different than the other tabs. For more information about how to create a drawable resource that handles multiple button states, read the State List documentation.
注:状态列表绘制很重要,这样当前选择的选项卡指示比其他标签不同背景的状态。有关如何创建一个处理多个按钮状态的可绘制资源的更多信息,请阅读状态列表文件。

For example, here's a state-list drawable that declares a specific background image for several different states of an action bar tab:
例如,这里有一个状态列表绘制声明一个操作栏选项卡的几种不同的状态特定的背景图片:

res/drawable/actionbar_tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected" />
    <item android: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) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected_focused" />
    <item android: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 -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android: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) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="true"
          android:drawable="@drawable/tab_selected_pressed" />
</selector>

 

For Android 3.0 and higher only
针对Android3.0和更高版本

 

When supporting Android 3.0 and higher only, your style XML file might look like this:
当支持Android的3.0和更高版本,你的风格XML文件可能是这样的:

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:actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

    <!-- ActionBar tabs styles -->
    <style name="MyActionBarTabs"
           parent="@style/Widget.Holo.ActionBar.TabView">
        <!-- tab indicator -->
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>
</resources>

 

For Android 3.0 and higher only
针对Android3.0和更高版本

 

When supporting Android 3.0 and higher only, your style XML file might look like this:
当支持Android的3.0和更高版本,你的风格XML文件可能是这样的:

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:actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

    <!-- ActionBar tabs styles -->
    <style name="MyActionBarTabs"
           parent="@style/Widget.Holo.ActionBar.TabView">
        <!-- tab indicator -->
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>
</resources>

 

For Android 2.1 and higher
针对Android2.1及更高

 

When using the Support Library, your style XML file might look like this:
当使用支持库,你的风格XML文件可能是这样的:

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.AppCompat">
        <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>

        <!-- Support library compatibility -->
        <item name="actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

    <!-- ActionBar tabs styles -->
    <style name="MyActionBarTabs"
           parent="@style/Widget.AppCompat.ActionBar.TabView">
        <!-- tab indicator -->
        <item name="android:background">@drawable/actionbar_tab_indicator</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_tab_indicator</item>
    </style>
</resources>

More resources
•See more style properties for the action bar are listed in the Action Bar guide.
•Learn more about how themes work in the Styles and Themes guide.
•For even more complete styling for the action bar, try the Android Action Bar Style Generator.
更多资源
•请参阅列在操作栏引导更多的样式属性的操作栏。
•了解更多关于主题的样式和主题指导如何工作的。
•为了更完整造型的操作栏,尝试了Android操作栏样式生成器。

本文翻译自:https://developer.android.com/training/basics/actionbar/styling.html

 

时间: 2024-10-29 10:25:38

Android官方入门文档[7]样式化操作栏的相关文章

Android官方入门文档[11]支持不同平台版本

Android官方入门文档[11]支持不同平台版本 Supporting Different Platform Versions支持不同平台版本   This lesson teaches you to1.Specify Minimum and Target API Levels2.Check System Version at Runtime3.Use Platform Styles and Themes You should also read•Android API Levels•Andr

Android官方入门文档

Android官方入门文档   欢迎来到为Android开发者的培训.在这里,你会发现套课中,描述了如何实现代码示例中的应用程序,你可以重复使用一个特定的任务类.类被组织成可以在左侧导航的顶层看到几个组.这第一组,入门,教你最基本的Android应用程序的开发.如果你是一个新的Android应用程序开发者,你应该按照顺序完成这些课程. 如果你认为你可能更喜欢通过互动视频培训学习的基础知识,看看这个预告片在Android开发的基础的一门课程. Getting Started   Welcome t

Android官方入门文档[5]建立操作栏

Android官方入门文档[5]建立操作栏   Setting Up the Action Bar建立操作栏 This lesson teaches you to1.Support Android 3.0 and Above Only2.Support Android 2.1 and Above You should also read•Setting Up the Support Library 这节课教你1.仅支持Android3.0及以上2.支持Android2.1及以上 你也应该阅读•设

Android官方入门文档[2]运行你的应用程序

Android官方入门文档[2]运行你的应用程序   Running Your App运行你的应用程序   This lesson teaches you to1.Run on a Real Device2.Run on the Emulator You should also read•Using Hardware Devices•Managing AVDs with AVD Manager•Managing Projects 这节课教你1.运行在真实设备2.运行在模拟器 你也应该阅读•使用硬

Android官方入门文档[4]启动另一个Activity

Android官方入门文档[4]启动另一个Activity   Starting Another Activity启动另一个Activity This lesson teaches you to1.Respond to the Send Button2.Build an Intent 3.Create the Second Activity4.Receive the Intent5.Display the Message You should also read•Installing the S

Android官方入门文档[16]创建一个Fragment代码片段

Android官方入门文档[16]创建一个Fragment代码片段   Creating a Fragment创建一个Fragment代码片段   This lesson teaches you to1.Create a Fragment Class2.Add a Fragment to an Activity using XML You should also read•Fragments 这节课教你1.创建一个Fragment代码片段类2.使用XML来添加一个Fragment代码片段给一个活

Android官方入门文档[9]支持不同的语言

Android官方入门文档[9]支持不同的语言 Supporting Different Languages支持不同的语言   This class teaches you to1.Create Locale Directories and String Files2.Use the String Resources You should also read•Localization Checklist•Localization with Resources 该课程教你1.创建区域设置目录和文件

Android官方入门文档[10]支持不同的屏幕

Android官方入门文档[10]支持不同的屏幕   Supporting Different Screens支持不同的屏幕   This lesson teaches you to1.Create Different Layouts2.Create Different Bitmaps You should also read•Designing for Multiple Screens•Providing Resources•Iconography design guide 这节课教你1.创建

Android官方入门文档[14]停止和重新启动一个Activity活动

Android官方入门文档[14]停止和重新启动一个Activity活动 Stopping and Restarting an Activity停止和重新启动一个Activity活动   This lesson teaches you to1.Stop Your Activity2.Start/Restart Your Activity You should also read•Activities 这节课教你1.停止您的Activity活动2.启动/重新启动您的Activity活动 你也应该阅