Override color value in color.xml file

狂风中的少年 提交于 2021-01-27 12:42:20

问题


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

Can I override the actual color of background in Java and set the @color/background to say - #FFFFFF?


回答1:


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.




回答2:


You can use .setBackgroundColor(getResources().getColor(R.color.myColor)); to set the background color in your Java code. This should override the xml background when it is set in the android:background xml tag, it did on my tested table row at least.



来源:https://stackoverflow.com/questions/16319900/override-color-value-in-color-xml-file

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