如何更改工具栏颜色

Sel*_*bis 10 android toolbar

我一直在搜索如何自定义工具栏,例如如何添加背景颜色,但我不明白它是如何工作的.

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

清单

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Design">
Run Code Online (Sandbox Code Playgroud)

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>    
    ...
Run Code Online (Sandbox Code Playgroud)

和布局中的工具栏

<android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>
Run Code Online (Sandbox Code Playgroud)

Sel*_*bis 17

谢谢,但任何解决方案都有效.

 <android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>
Run Code Online (Sandbox Code Playgroud)

要么

toolbar.setBackgroundColor(Color.parseColor("#80000000"));
Run Code Online (Sandbox Code Playgroud)

可能是因为我的工具栏在android.support.design.widget.CoordinatorLayout(放一个android.support.design.widget.FloatingActionButton)?


ian*_*ake 5

实际上,有一个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"/>
Run Code Online (Sandbox Code Playgroud)

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


Adi*_*ale 5

从资源文件设置颜色

toolbar.setBackgroundColor(getResources().getColor(R.color.red));
Run Code Online (Sandbox Code Playgroud)


小智 1

用这个

toolbar.setBackgroundColor((Color.parseColor("#80000000")));
Run Code Online (Sandbox Code Playgroud)