工具栏不消耗主题属性

Toolbar does not consume theme attributes

我用主题

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

工具栏看起来像这样

但对于 CoordinatorLayour,我需要自定义工具栏,而不是最顶层元素(最顶层将折叠布局),所以我使用以下样式 Activity:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

以及 activity.xml

中的工具栏
<android.support.v7.widget.Toolbar
    android:id="@+id/ac_main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/action_bar_height"
    tools:title="title"
    app:theme="@style/AppTheme"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    app:layout_collapseMode="pin"/>

但它没有填充来自 AppTheme 的颜色。看起来像这样

为什么?我需要我的主题颜色和工具栏中心的点。

尝试将 android:background="@color/primary" 添加到您的工具栏定义中。 喜欢:

android.support.v7.widget.Toolbar
android:id="@+id/ac_main_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_height"
android:background="@color/primary"
tools:title="title"
app:theme="@style/AppTheme"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:layout_collapseMode="pin"/> 

将以下内容添加到您的主题中:

<!-- toolbar title and overflow menu color -->
<item name="android:textColorSecondary">@color/white</item>

对于垂直居中对齐,您必须使用工具栏的高度,如下所示:

<android.support.v7.widget.Toolbar
    android:id="@+id/ac_main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"    <!-- Height change -->
    tools:title="title"
    app:theme="@style/AppTheme"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    app:layout_collapseMode="pin"/>

经过一番研究,我发现工具栏主题不是 AppCompat 主题,应该是 ThemeOverlay。*

对于深色操作栏主题,我应该使用

<style name="BarTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

not from parent="Theme.AppCompat.Light.DarkActionBar"

这解决了所有颜色问题。

修复标题和 3 个点(将其放在工具栏中间)- AppBarLayout 高度应为 wrap_content(之前是固定大小),并且以像素为单位的折叠大小应移至 AppBarLayout 下的 CollapsingToolbarLayout。