How to change toolbar color

只愿长相守 提交于 2020-01-02 01:07:18

问题


I've been searching how to customize the toolbar, for example how to add background color, but I don't understand how it works.

I've been trying to add a custom style for my toolbar but any result ...

The Manifest

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

The style.xml file

<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>    
    ...

And the toolbar in layout

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

回答1:


Thanks, but any solution works.

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

or

toolbar.setBackgroundColor(Color.parseColor("#80000000"));

May be because my toolbar is in android.support.design.widget.CoordinatorLayout (to put a android.support.design.widget.FloatingActionButton) ?




回答2:


In fact, there was a Android Developers pro-tip which go into details on how to color the Toolbar using colorPrimary.

You were definitely on the right track, adding colorPrimary to your theme. What you need is to set the background on the Toolbar:

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

Note, if you have a dark colorPrimary and a light theme, you'll need to also add android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" to ensure the text and icons are white over the dark background.




回答3:


set Color from resource file

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



回答4:


You can set the background in the 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"
/>



回答5:


Use this

toolbar.setBackgroundColor((Color.parseColor("#80000000")));



回答6:


Try to use

<item name="android:windowBackground">@color/primary</item>

in your styles. It's the same tag name with window background, but changes the toolbar background color only when you use it with your toolbar styles.



来源:https://stackoverflow.com/questions/32237284/how-to-change-toolbar-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!