Problem setting TextView color
Here is my color XML
<resources>
<drawable name="red">#7f00</drawable>
<drawable name="blue">#770000ff</drawable>
<drawable name="green">#7700ff00</drawable>
<drawable name="yellow">#77ffff00</drawable>
<drawable name="screen_background_black">#ff000000</drawable>
<drawable name="translucent_background">#e0000000</drawable>
<drawable name="transparent_background">#00000000</drawable>
<color name="solid_red">#ED1C24</color>
<color name="solid_blue">#0000ff</color>
<color name="solid_green">#39B54A</color>
<color name="solid_yellow">#ffffff00</color>
</resources>
Here is my Java code:
if (floatedChange < 0)
changeText.setTextColor(R.color.solid_red); //red
else
changeText.setTextColor(R.color.solid_green); //green
This works 开发者_运维问答when I use Color.RED or Color.GREEN, but when I use my own colors. The color doesn't show up.
Why?
Sorry about answering an old question, but here is the correct answer for others with the same problem:
From http://developer.android.com/guide/topics/resources/more-resources.html#Color
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
So just change your Java code to
if (floatedChange < 0)
changeText.setTextColor( getResources().getColor(R.color.solid_red) ); //red
else
changeText.setTextColor( getResources().getColor(R.color.solid_green) ); //green
Do like this:
In your XML-file, use style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourCustomText">
<item name="android:textColor">#FFFF00</item>
</style>
</resources>
then, in your .java code:
//Here you assign the style that is defined in YourCustomText
yourTextView.setTextAppearance(this, R.style.YourCustomText);
精彩评论