How to change toolbar color

可紊 提交于 2019-12-05 02:53:44

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) ?

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.

Aditya varale

set Color from resource file

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

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"
/>

Use this

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

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.

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