如何更改工具栏颜色

19

我一直在寻找自定义工具栏的方法,例如如何添加背景颜色,但是我不理解它是如何工作的。

我一直在尝试为我的工具栏添加自定义样式,但是没有任何结果...

清单

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Design">

样式表(style.xml)文件

<resources>

    <style name="Theme.Design" parent="Base.Theme.Design">
    </style>

    <style name="Base.Theme.Design" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryDark">@color/red</item>
        <item name="colorAccent">@color/red</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
    </style>    
    ...

布局中的工具栏

<android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>
7个回答

27

谢谢,但任何解决方案都不起作用。

 <android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>
或者
toolbar.setBackgroundColor(Color.parseColor("#80000000"));

可能是因为我的工具栏在 android.support.design.widget.CoordinatorLayout 中(用于添加 android.support.design.widget.FloatingActionButton)?


8
事实上,Android开发者的专业提示详细介绍了如何使用colorPrimary对工具栏进行着色。
你肯定在正确的方向上,已将colorPrimary添加到你的主题中。需要做的是在工具栏上设置背景:
<android.support.v7.widget.Toolbar
        android:id="@+id/home_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

注意,如果你有一个深色的colorPrimary和一个浅色的主题,你还需要添加android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar",以确保文本和图标在深色背景上是白色的。


1
但是,由于我在整个应用程序中使用常见的工具栏,并且有时我们希望在同一工具栏的某个地方使用不同的样式,特别是渐变背景,因此我该如何动态地实现它?这还应该允许我更改工具栏标题或位于该工具栏下方的文本视图的字体样式! - Sagar Patel

5

从资源文件中设置颜色

toolbar.setBackgroundColor(getResources().getColor(R.color.red));

2
尝试使用。
<item name="android:windowBackground">@color/primary</item>

在你的样式中,它是与窗口背景相同的标签名称,但仅在与工具栏样式一起使用时更改工具栏背景颜色。


1
使用这个。
toolbar.setBackgroundColor((Color.parseColor("#80000000")));

谢谢你,但它没有起作用。可能是因为我在一个CoordinatorLayout中? - Selorbis

1

你可以在XML中设置背景。

<android.support.v7.widget.Toolbar
        android:id="@+id/home_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
/>

1
谢谢,但它不起作用。可能是因为我在 CoordinatorLayout 中? - Selorbis

0

我的答案放在你的styles.xml文件中:

 <item name="android:textColor">@color/colorPrimaryDark</item>

就像我的文件夹中:

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.NoActionBar">

<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:fontFamily">@font/asap</item>
<item name="android:textColor">@color/colorPrimaryDark</item>

</style>

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