Styling the Action Bar --1.1.3

尽管 Action bar 很符合人们的操作习惯,但是原生(默认) Action bar 并不一定适合你的应用程序。因此,如果你想让 Aciton bar 的样式更符合你应用程序的风格,可以为你的 Action bar 定义样式。

Android 系统中为每一个 Activity 都预定义了一个主题(”dark” 或者 “light”)。 你可以继承这些已有的主题,然后进行扩展,从而实现自定义的主题,从而为 Action bar 定义不同的样式。

注意:如果你是通过扩展库实现的 Action bar,那么你声明的主题必须继承自Theme.AppCompat或者重写这个主题(因为 Theme.Holo只能在 Android 版本大于、等于11以上才可以使用)。

1. Use an Android Theme

Android 默认为 Activity 提供了两种主题:

1. Theme.Holo (暗)
2. Theme.Holo.Light (亮)

你可以将这些主题应用在整个应用程序中,也可以单独应用在某个 Activity 上(只需要在清单文件里给<application>或者activity元素加上相应的主题即可)。例如:

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

如果你在创建 Action bar 时,使用的是支持库,那么你必须使用Theme.AppCompat系列主题中的一个:

1. Theme.AppCompat (暗)
2. Theme.AppCompat.Light (亮)
3. Theme.AppCompat.Light.DarkActionBar (主题是亮的,但是 Action bar 是暗的)

注意:一定合理搭配你的 Action bar 和 上面图标的颜色,否则,用户不会太喜欢的。

2. Customize the Background

为了改变 Action bar 的背景颜色,你需要重写actionBarStyle属性。这个属性指向其他的样式,在这个样式里,你可以重写background属性,从而为 Action bar 指定新的背景。

注意:在重新定义 Action bar 属性的时候,你必须合理的处理自定义主题和系统主题之间的关系,如果你的自定义主题没有继承现有主题,那么你必须在自定义的主题里面声明一些属性,否则那些没设置的属性将不会作用在 Action bar 上。

1. For Android 3.0 and higher only

当你为 Android 版本为 11 及以上添加 Action bar 时,你可以像下面这样做:

<?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>

然后将这个主题应用在整个应用程序中,或者单个 Activity上:

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

2. For Android 2.1 and higher

如果你的 Android 版本不是 3.0,而是 2.1,而且想达到上面同样的效果,你可以这样:

<?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>

然后将这个主题应用在整个应用程序中,或者单个 Activity上:

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

3. Customize the Text Color

为了更改 Action bar 中文本的颜色,你需要单独重写每一个文本元素的样式:

Action bar title:

创建一个自定义样式A,指定textColor属性,然后在之前创建的自定义样式MyActionBar中指定titleTextStyle属性,并引用自定义样式A。

注意:titleTextStyle所引用的自定义样式应该继承自TextAppearance.Holo.Widget.ActionBar.Title

Action bar tabs:

重写actionBarTabTextStyle属性。

Action buttons:

重写actionMenuTextColor属性。

1. For Android 3.0 and higher only

当你为 Android 版本为 11 及以上添加 Action bar 时,你可以像下面这样做:

<?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>

2. For Android 2.1 and higher

如果你的 Android 版本不是 3.0,而是 2.1,而且想达到上面同样的效果,你可以这样:

<?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>

4. Customize the Tab Indicator

为了改变导航栏的指示器,在为 Activity 创建自定义主题的时候,你应该重写actionBarTabStyle属性。这个属性将指向一个自定义样式,通过在那个自定义样式文件里面指定一系列( state-list drawable 状态表图片资源)的drawable
文件即可实现自定义指示器。

注意:state-list drawable是一系列的图片资源文件,它们根据当前控件的状态而显示不同的图片资源文件。

下面就是一个state-list drawable,它为不同状态的 Action bar tab 提供了不同的图片资源:

<?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>

1. For Android 3.0 and higher only

当你为 Android 版本为 11 及以上添加 Action bar 时,你可以像下面这样做:

<?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>

2. For Android 2.1 and higher

如果你的 Android 版本不是 3.0,而是 2.1,而且想达到上面同样的效果,你可以这样:

<?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>

好了,这篇文章到这里就结束了,希望能帮到小伙伴,have a good day~

你可能感兴趣的:(android,基础,练习,自定义样式,action-bar)