更改Material Design AppCompat ActionBar颜色

29

以前我使用 actionBarStyle 更改 AppCompat 状态栏颜色,创建一个带有所需颜色背景的样式。

现在,使用 Material Design AppCompat,这种方法不再起作用。

您能帮我吗?谢谢。


你可能是在指ActionBar吗?只是为了澄清一下你的意思。 - Nikola Despotoski
@NikolaDespotoski 我有一个ActionBarActivity,必须使用AppCompat样式。所以,我所做的是在AppCompat样式中添加了一个名为actionBarStyle的属性,并且我在那里设置的样式具有更改操作栏不同颜色的背景。 - user3184899
@NikolaDespotoski 需要帮忙吗? - user3184899
5个回答

89

现在有一个名为colorPrimary的新属性,您可以在自己的主题中定义它。这将使您的操作栏或工具栏拥有一个纯色。

以下是一个简单的示例:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/my_action_bar_color</item>
</style>

请注意:除了values-v21文件夹之外的每个值文件夹中,都只能使用colorPrimary而不是android:colorPrimary

您可以在developer.android.com上阅读有关自定义颜色调色板的更多信息。


1
颜色主题适用于 Api 20 及以上版本。但在 Kitkat 上,这对我来说根本没有任何作用。 - Paul Woitaschek
8
不是这样的。colorPrimaryv7-appcompat 的一部分,因此基本上适用于 API-Level 7 及以上版本(请注意父级 Theme.AppCompat)。 - reVerse
1
请查看使用Material Design创建应用程序-->维护兼容性 ,您可以在那里找到一个例子。但我认为目前没有明确描述colorPrimary属性的文档。 - reVerse
1
@KevinChris,你使用了android:colorPrimary,但实际上你只需要colorPrimary(没有android:前缀)。再看一下我的帖子。 ;) - reVerse
对我不起作用。http://stackoverflow.com/questions/29248542/change-status-bar-color-on-api-below-21-android - WISHY
显示剩余10条评论

2
在我的情况下,@reVerse 提供了部分答案。我需要进行一些额外的更改才能使 colorPrimary 生效,因为我正在自定义工具栏的其他部分...特别是文本颜色。
以下是我的 styles.xml 文件,其中包含注释,指出我做错了什么:
<!-- As @reVerse mentioned, you need to use colorPrimary instead of android:colorPrimary -->
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/my_toolbar_color</item>
</style>

<!-- I was incorrectly using @style/Theme.AppCompat.Light for the popupTheme attribute -->
<style name="MyToolbarStyle" parent="Widget.AppCompat.ActionBar">
    <item name="theme">@style/MyToolbarTextStyle</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
</style>

<!-- I was incorrectly using Them.AppCompat.Light for the parent here -->
<style name="MyToolbarTextStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/my_toolbar_text_color</item>
</style>

因此,在自定义工具栏背景颜色时,您需要确保使用ThemeOverlay主题之一,否则由于某种原因,colorPrimary将被忽略。

为了完整起见,这是我用于我的工具栏的布局文件Toolbar.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schmeas.android.com/apk/res-auto"
    style="@style/MyToolbarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

希望这能帮助到某些人!


1

Theme.MaterialComponents

<style name="Theme.EmojiBible" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="actionBarTheme">@style/ThemeOverlay.Actionbar</item>
</style>

<style name="ThemeOverlay.Actionbar" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" >
    <item name="android:textColorPrimary">#f00</item>
</style>

-1
   <style name="AppTheme" parent="Theme.AppCompat.Light">

        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <!-- Customize your theme here. -->
    </style>

另请参阅:制作自定义操作栏


-1

在文档中设置应用栏的指南建议在Android清单文件中像这样设置主题:

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

然而,根据我的经验,这会覆盖为自定义AppTheme设置的colorPrimary样式。对我有效的方法是定义一个自定义主题,如下所示:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

然后在 AndroidManifest 文件中使用此主题。

<application
    android:theme="@style/AppTheme"
    />

之后根据colorPrimary在活动中设置工具栏背景颜色就可以正常工作了。

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

(使用深色主题设置ThemeOverlay可以确保文本呈现为浅色。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接