更改工具栏背景颜色编程方式不会改变工具栏标题背景颜色 [英] Change Toolbar background color programmatically does not change Toolbar Title Background color

查看:123
本文介绍了更改工具栏背景颜色编程方式不会改变工具栏标题背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm试图通过这样做编程方式更改的工具栏背景颜色:

<$p$p><$c$c>getSupportActionBar().setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.test_color_blue)));

这是结果:

有些工具栏标题怎么还像以前一样有相同的背景颜色。

这是我的工具栏上的xml:

 &LT; android.support.v7.widget.Toolbar的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:重力=center_vertical
应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
应用程序:主题=@风格/ Theme.Toolbar&GT;
 

这里是主题:

 &LT;样式名称=Theme.Toolbar父=ThemeOverlay.AppCompat.Dark.ActionBar&GT;
    &LT;项目名称=安卓了maxHeight&GT; @扪/ abc_action_bar_default_height_material&LT; /项目&GT;
    &LT;项目名称=机器人:背景&GT; @色/初级&LT; /项目&GT;
    &LT;项目名称=colorPrimary&GT; @色/初级&LT; /项目&GT;
    &LT;项目名称=colorPrimaryDark&GT; @色/ primary_dark&LT; /项目&GT;
    &LT;项目名称=colorAccent&GT; @android:彩色/白&LT; /项目&GT;
    &LT;项目名称=titleTextAppearance&GT; @风格/ Theme.Toolbar.Title&LT; /项目&GT;
&LT; /风格&GT;
 

解决方案

更​​改code如下:

toolbar.xml

 &LT; android.support.v7.widget.Toolbar的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        风格=@风格/ MyToolbarStyle
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_vertical&GT;
 

主题/风格

 &LT;样式名称=MyToolbarStyle&GT;
    &LT;项目名称=安卓了maxHeight&GT; @扪/ abc_action_bar_default_height_material&LT; /项目&GT;
    &LT;项目名称=机器人:背景&GT; @色/初级&LT; /项目&GT;
    &LT;项目名称=popupTheme&GT; @风格/ ThemeOverlay.AppCompat.Light&LT; /项目&GT;
    &LT;项目名称=titleTextAppearance&GT; @风格/ Theme.Toolbar.Title&LT; /项目&GT;
    &LT;! - 无需colorPrimary,colorPrimaryDark,colorAccent这里
         这应该去AppTheme  - &GT;
&LT; /风格&GT;
 

结果

在设置新的背景颜色:

和后:

I´m trying to change the toolbar Background color programmatically by doing this:

getSupportActionBar().setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.test_color_blue)));

And this is the result:

before:

After:

Some how the toolbar title still has the same background color as before.

here is my toolbar xml:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/Theme.Toolbar">

And here is the Theme:

<style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
    <item name="android:background">@color/primary</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@android:color/white</item>
    <item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
</style>

解决方案

Change your code as follows:

toolbar.xml

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

Theme / Style

<style name="MyToolbarStyle">
    <item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
    <item name="android:background">@color/primary</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
    <!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
         this should go to the AppTheme -->
</style>

Result

Before setting the new background color:

and after:

这篇关于更改工具栏背景颜色编程方式不会改变工具栏标题背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆