Android - 如何在程序中设置按钮颜色

10

我正在从REST API中读取一些数据,并根据应用程序接收到的信息生成一些按钮。

因为我需要在许多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)。

但是像这样设置文本颜色根本没有任何效果,按钮上的文本颜色似乎是按钮背景颜色的较深色调。

如果有人能给我一些关于下一步尝试的建议,我将非常感激。


你应该看一下Android主题和样式。它们允许你将相同的外观和感觉应用于各种类型的一个、多个或所有UI元素。 - Christopher Orr
我刚开始了解这个。 - Rachel
3个回答

44

你应该这样做:

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

3
API 23 开始,您应该使用 ContextCompat.getColor(context, R.color.your_color); 来获取颜色。getcolorint-id-deprecated-on-android-6-0-marshmallow-api-23 - Simon Featherstone

3

如果您有View对象(从R类中的findViewById)转换为特定对象,如Button,那就更好了。(标准方法 - Button b = (Button) findViewById(R.id.sdfsdf)

接下来只需从几个Android颜色中选择:

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

或更好:从RGB开始
 b.setTextColor(Color.rgb(0xff, 0x66, 0x33));

在Eclipse中,所有操作都可以通过ctrl+spaceBar实现 :P


抱歉!也许b.setTextColor(0xff0000)也能起作用...


0

从API级别23开始,getColor()函数已被弃用。请查看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);
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接