问题
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