Android的工具栏颜色变化 [英] Android Toolbar color change
问题描述
我有一个使用 android.support.v7.widget.Toolbar
的应用程序。该应用程序的每一个部分是一个片段
通过访问的 support.v4.widget.DrawerLayout
I have an app which uses android.support.v7.widget.Toolbar
. Every section of the app is a Fragment
accessed through a support.v4.widget.DrawerLayout
我需要改变工具栏
颜色取决于其显示部分(客户的特定需求)。
I need to change the Toolbar
color depending on which section is shown (client particular needs).
我定义的一些颜色 colors.xml
这样我就可以做这样的:
I defined some colors in the colors.xml
so I can make something like:
changeToolbarColor(R.color.section_one);
/**/
private void changeToolbarColor(int color_res_id){
Integer colorTo = getResources().getColor(color_res_id);
toolbar.setBackgroundColor(colorTo);
}
问题是,一旦我做到这一点,使用 primaryColor
(原来的主工具栏上的颜色)每个视图现在显示了使用新颜色的工具栏。
The problem is, once I do this, every view using the primaryColor
(the original primary color from the toolbar) now shows up using the new color of the Toolbar.
所以,如果我的工具栏是的绿色的,我将其更改为的红色的,现在的所有的使用旧的绿色使用红色来代替。
So if my Toolbar was green and I change it to red, now everything using the old green uses red instead.
我怀疑,该工具栏背景的变化而变化的 primaryColor
定义本身(这是没有意义的我)。因为我有一个如何在不相关的活动不相关的元素开始使用相同的颜色没有别的想法。
I suspect, that the change of the Toolbars background changes the primaryColor
definition itself (which makes no sense to me). Because I have no other idea of how unrelated elements in unrelated activities start using the same color.
这是一个错误吗?任何人只要有这个问题?任何变通办法可用?
Is this a bug? Anyone with this problem? Any workarounds available?
感谢您的帮助。
推荐答案
所有的主题首先是一成不变,所以它不可能改变应用的主要颜色。
First of all themes are immutable, so it's not possible to change the primary color of the app.
和尝试使用 getSupportActionBar()。setBackgroundDrawable()
。
我想这是别的东西,这是造成问题。你能发布更多code?
I guess it's something else which is causing the issue. Can you post more code ?
这篇关于Android的工具栏颜色变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!