我正在从REST api读取一些数据,需要根据应用程序收到的信息生成一些按钮.
因为我在许多活动屏幕中需要相同的按钮,我已经扩展了按钮来创建一个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).
但是设置这样的文本颜色根本没有任何效果,按钮上的文本颜色似乎是按钮背景颜色的阴影.
如果有人可以给我一些关于接下来要尝试的建议,我将是最感激的.
解决方法
你应该做:
setTextColor(getContext().getResources().getColor(R.color.info_button_text_color));