Android的 - 如何编程设置按钮颜色 [英] Android - How to programmatically set button color
问题描述
我是从一个REST API中的一些数据读取,需要根据应用程序接收到的信息来生成一些按钮。
I am reading in some data from a REST api and need to generate some buttons based on the information the app receives.
由于我需要很多的活动画面我已经扩展按钮进行RachelButton相同的按钮和我在构造函数中进行设置。
Because I need the same buttons in many Activity screens I have extended Button to make a RachelButton and I set it up in the constructor.
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
}
});
}
然后我使用生成的彩色按钮资源的一个例子是这样的:
Then an example of the resource I am using to generate a colored button is this:
<?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>
正如你可以在code见我设置文本颜色,我敢肯定,这种颜色的存在是为资源(谢谢你的IntelliJ)。
As you can see in the code I am setting the text color and I'm sure that this color exists as a resource (thank you IntelliJ).
但设置这样的文字颜色没有效果可言,按钮上的文字颜色似乎是按钮的背景颜色暗的阴影。
But setting the text color like this has no effect at all, the text color on the button seems to be a darker shade of the button's background color.
如果任何人都可以给我一些建议,以什么尝试下一次我会是最AP preciative。
If anyone could give me some advice as to what to try next I would be most appreciative.
推荐答案
您应该做的:
setTextColor(getContext().getResources().getColor(R.color.info_button_text_color));
这篇关于Android的 - 如何编程设置按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!