覆盖color.xml文件中的颜色值 [英] Override color value in color.xml file
问题描述
有没有一种方法可以覆盖color.xml文件中已经设置的颜色?例如-我将布局上的背景色设置为backgroundColor ="@ color/background" background的值=#000000
Is there a way to override a color that is already set in your color.xml file? For example - I set the background color on the layout to backgroundColor = "@color/background" The value of background = #000000
我可以在Java中覆盖背景的实际颜色,并将@ color/background设置为-#FFFFFF吗?
Can I override the actual color of background in Java and set the @color/background to say - #FFFFFF?
推荐答案
可能,但代价很高.@ color/background等效于R.color.background,其中R是您生成的类.在运行时,您可以使用反射以使字段可写,然后更改它们.我真的不相信它会按您的意愿工作.
It's possible, but rather costly. @color/background is equivalent to R.color.background, where R is your generated class. At runtime, you could use reflection to make the fields writable, then change them. I'm not really convinced it would work how you want.
一种不同的想法是使用主题您想要的颜色,可能会让用户在它们之间进行切换.这样可以使您的设计和逻辑分离.
A different idea would be to use themes for the various colors you want, possibly letting the user switch between them. That'd keep your design and logic separate.
这篇关于覆盖color.xml文件中的颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!