2010-07-22 70 views
9

我正在閱讀REST api中的某些數據,並需要根據應用程序接收的信息生成一些按鈕。Android - 如何以編程方式設置按鈕顏色

因爲我需要在很多Activity屏幕中使用相同的按鈕,所以我擴展了Button來創建一個RachelButton,並在構造函數中設置它。

public RachelButton(Context context, Info info) { 
    super(context); 
    this.info= info; 

    setText(info.getTime()); 
    setTypeface(Typeface.DEFAULT, Typeface.BOLD); 

    int identifier = 0; 

    if(info.isAvailable()){ 
     identifier = getContext().getResources().getIdentifier("drawable/info_button_"+info.getType(), null, getContext().getPackageName()); 
    }else{ 
     identifier = R.drawable.info_button_unavailable; 
    } 

    if(identifier == 0){ 
     Log.e("INFO_BUTTON", "no button for "+info.getType()); 
    } 

    setBackgroundResource(identifier); 
    setTextColor(R.color.info_button_text_color); 

    setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view) { 
      //do stuff 
     } 
    }); 
} 

然後我使用生成的彩色按鈕資源的一個例子是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" > 
    <shape> 
     <gradient 
      android:startColor="@color/button_pressed" 
      android:endColor="@color/button_pressed" 
      android:angle="270" /> 
     <stroke 
      android:width="3dp" 
      android:color="@color/button_pressed" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp" /> 
    </shape> 
</item> 

<item android:state_focused="true" > 
    <shape> 
     <gradient 
      android:endColor="@color/info_normal" 
      android:startColor="@color/info_normal" 
      android:angle="270" /> 
     <stroke 
      android:width="3dp" 
      android:color="@color/info_normal" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp" /> 
    </shape> 
</item> 

<item> 
    <shape> 
     <gradient 
      android:endColor="@color/info_normal" 
      android:startColor="@color/info_normal" 
      android:angle="270" /> 
     <stroke 
      android:width="3dp" 
      android:color="@color/info_normal" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp" /> 
    </shape> 
</item> 
</selector> 

正如你可以在代碼中看到我設置的文本顏色,我敢肯定,這種顏色作爲資源存在(謝謝IntelliJ)。

但是,像這樣設置文本顏色根本沒有效果,按鈕上的文本顏色似乎是按鈕背景顏色的較暗陰影。

如果有人可以給我一些建議,接下來要做什麼,我會非常感激。

+0

你應該看看Android的主題和風格。它們允許您將相同的外觀和感覺應用於各種類型的一個,多個或所有UI元素。 – 2010-07-26 10:50:46

+0

我剛開始閱讀。 – Rachel 2010-07-26 12:38:11

回答

43

你應該這樣做:

setTextColor(getContext().getResources().getColor(R.color.info_button_text_color)); 
+0

謝謝 - 完美的作品:) – Rachel 2010-07-26 12:39:03

+0

感謝您的支持 – SaKet 2011-06-29 22:56:03

+3

從** API 23 **開始,您會希望使用'ContextCompat.getColor(context,R.color.your_color);'[getcolorint-id-deprecated-on -Android-6-0-棉花糖API-23](http://stackoverflow.com/questions/31590714/getcolorint-id-deprecated-on-android-6-0-marshmallow-api-23) – 2016-07-01 11:42:45

3

更好,如果你有(來自R類findViewById)View對象轉換信息的特定對象:例如按鈕。 (標準方法 - Button b = (Button) fin...(R.id.sdfsdf)

下一個剛剛從幾雄,顏色類型:

b.setTextColor(Color.parseColor("green")); 

或更好:從RGB

b.setTextColor(Color.rgb(0xff, 0x66, 0x33)); 

一切都是在Eclipse的ctrl+spaceBar:P


對不起!也許b.setTextColor(0xff0000)也可以工作...

0

getColor()函數已從API級別23棄用。請檢查此link瞭解更多詳細信息。
下面是支持庫的源代碼:

public static final int getColor(Context context, int id) { 
    final int version = Build.VERSION.SDK_INT; 
    if (version >= 23) { 
     return ContextCompatApi23.getColor(context, id); 
    } else { 
     return context.getResources().getColor(id); 
    } 
} 
相關問題