Android - 如何以编程方式设置按钮颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3310603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:39:02  来源:igfitidea点击:

Android - How to programmatically set button color

android

提问by Rachel

I am reading in some data from a REST api and need to generate some buttons based on the information the app receives.

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

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.

因为我需要在许多活动屏幕中使用相同的按钮,所以我扩展了 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
        }
    });
}

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>

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).

正如您在代码中看到的那样,我正在设置文本颜色,并且我确定该颜色作为资源存在(感谢 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.

但是像这样设置文本颜色根本没有效果,按钮上的文本颜色似乎是按钮背景颜色的较暗阴影。

If anyone could give me some advice as to what to try next I would be most appreciative.

如果有人能给我一些关于下一步尝试什么的建议,我将不胜感激。

回答by pakerfeldt

You should do:

你应该做:

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

回答by Volodia

Better if you have the View object (findViewByIdfrom R class) transformed info specific object: for example Button. ( the standard way - Button b = (Button) fin...(R.id.sdfsdf))

如果您有 View 对象(findViewById来自 R 类)转换信息特定对象:例如 Button。(标准方式 - Button b = (Button) fin...(R.id.sdfsdf)

Next just type from a few andro-colors:

接下来只需输入一些 andro 颜色:

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

or BETTER: FROM RGB

或更好:来自 RGB

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

Everything is in the ctrl+spaceBarin Eclipse :P

一切都ctrl+spaceBar在 Eclipse 中:P



Sorry! Maybe the b.setTextColor(0xff0000)would also works...

对不起!也许b.setTextColor(0xff0000)这也有效......

回答by Steph Noutsa

The getColor() function is deprecated from API level 23. Check this linkfor more details.
Below is the source code from the support library:

从 API 级别 23 开始不推荐使用 getColor() 函数。查看此链接了解更多详细信息。
下面是来自支持库的源代码:

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);
   }
}