Android 支持工具栏样式

Android Support Toolbar Styling

我正在尝试使用深色工具栏(操作栏)实现 AppCompat 的浅色主题,但是当动态添加工具栏或使用 <include /> 时,文本无法以正确的颜色显示(黑色而不是白色的)。默认操作栏的样式正确,但当我添加工具栏时却不正确。

这是我的代码:

toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/AppTheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    />

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#ff299725</item>
        <item name="colorPrimaryDark">#ff1d691b</item>
        <item name="colorAccent">#ff5fb10b</item>
    </style>

</resources>

但是我在预览和实时环境中得到这个:

我尝试了不同版本的 AppCompat(v.22.1、v.22.2、v.21.0.3)都重复了这个问题,我尝试为 textColor 添加额外的样式,结果是它将所有样式设置为白色。

有什么帮助吗?

styles.xml

<style name="Toolbar" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    <item name="android:textColorPrimary">@color/xxx</item>
    <item name="android:background">@color/xxx</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@color/xxx</item>
    <item name="gapBetweenBars">4dp</item>
</style>

my_toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/Toolbar"
    />

在您的 toolbar.xml 中,删除 app:theme 属性并改用它:

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

此外,根据您实现工具栏的方式,您可能希望更改基本主题(在 styles.xml 中)以使用 NoActionBar 变体,如下所示:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">