2010-07-22 71 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); 
    } 
} 
相关问题