2010-09-24 40 views
1

這裏是我的顏色XML問題設置TextView的顏色

<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,但是當我用我的自己的顏色。顏色不顯示。

爲什麼?

回答

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); 
+0

該礦位於/res/values/color.xml – 2010-09-24 17:02:05

+0

你有沒有在一開始的 「<?xml的...」 標籤? – Wroclai 2010-09-24 17:04:16

+0

我在開頭有<?xml標籤。我將我的代碼更改爲:changeText.setTextColor(R.style.solid_red); //但是solid_red找不到 – 2010-09-24 17:08:29

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

這是一個更好的答案,因爲它直接從資源獲取顏色,而不僅僅是創建新的樣式資源 – 2011-05-20 19:41:18

相關問題