这是我的颜色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>
这是我的Java代码:
if (floatedChange < 0)
changeText.setTextColor(R.color.solid_red); //red
else
changeText.setTextColor(R.color.solid_green); //green
当我使用Color.RED或Color.GREEN时,这是有效的,但是当我使用自己的颜色时。颜色没有显示出来。
为什么?
答案 0 :(得分:3)
对于回答旧问题感到抱歉,但对于遇到同样问题的其他人来说,这是正确答案:
来自http://developer.android.com/guide/topics/resources/more-resources.html#Color
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
所以只需将您的Java代码更改为
即可if (floatedChange < 0)
changeText.setTextColor( getResources().getColor(R.color.solid_red) ); //red
else
changeText.setTextColor( getResources().getColor(R.color.solid_green) ); //green
答案 1 :(得分:2)
这样做:
在您的XML文件中,使用样式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourCustomText">
<item name="android:textColor">#FFFF00</item>
</style>
</resources>
然后,在你的.java代码中:
//Here you assign the style that is defined in YourCustomText
yourTextView.setTextAppearance(this, R.style.YourCustomText);