如何在代码中设置TextView的文本颜色?

613
在XML中,我们可以通过textColor属性设置文本颜色,例如android:textColor="#FF0000"。但是我该如何通过编码来更改它呢?
我尝试了以下内容:
holder.text.setTextColor(R.color.Red);

holder 是一个类,textTextView 类型。 红色是在字符串中设置的 RGB 值(#FF0000)。

但它显示的颜色与红色不同。 我们可以在 setTextColor() 中传递什么参数? 在文档中,它说是 int,但它是资源引用值还是其他任何东西?


关于在代码中调整用户界面的注意事项,请考虑在设计时查看用户界面的优点,将运行时更改最小化。 - AlikElzin-kilaka
40个回答

1372

你应该使用:

holder.text.setTextColor(Color.RED);

当然,您可以使用Color类中的各种函数来获得相同的效果。

  • Color.parseColor (Manual) (like LEX uses)

    text.setTextColor(Color.parseColor("#FFFFFF"));
    
  • Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses)

    holder.text.setTextColor(Color.rgb(200,0,0));
    holder.text.setTextColor(Color.argb(0,200,0,0));
    
  • And of course, if you want to define your color in an XML file, you can do this:

    <color name="errorColor">#f00</color>
    

    because the getColor() function is deprecated1, you need to use it like so:

    ContextCompat.getColor(context, R.color.your_color);
    
  • You can also insert plain HEX, like so:

    myTextView.setTextColor(0xAARRGGBB);
    

    Where you have an alpha-channel first, then the color value.

请查看完整的课程手册,public class Color extends Object


1这段代码过去也在这里:

textView.setTextColor(getResources().getColor(R.color.errorColor));

这种方法在Android M中已被弃用。但是,您可以从支持库中的contextCompat中使用它,就像现在的示例一样。


2
此外,如果文本是一个链接,您需要在代码中使用text.setLinkTextColor(...)或在XML中使用android:textColorLink="..."。 - WOUNDEDStevenJones
1
@Nanne 如果你的回答也提到 R.color.XXX 是颜色的引用,那就太好了。这意味着它需要被取消引用(就像在你的示例中一样),以增加清晰度。 - nyaray
8
getColor(int) 已被弃用。 - RestInPeace
1
抱歉,我的表述不够清晰。在Android 6.0(API 23)中,“getResource().getColor()”已被弃用。请考虑改用“ContextCompat.getColor()”。 - Alwin Kesler
1
如何导入颜色类? - jorghe94
显示剩余6条评论

149

如果您仍然想在XML文件中指定颜色:

<color name="errorColor">#f00</color>

接下来,在你的代码中使用以下两种方法之一引用它:

textView.setTextColor(getResources().getColor(R.color.errorColor, getResources().newTheme()));    
或者
textView.setTextColor(getResources().getColor(R.color.errorColor, null));

如果您正在编译针对Android M的应用程序,则可能首选第一种方法,但您传递的主题可以为空,因此这可能更容易?

如果您使用Compat库,可以尝试类似以下代码:

textView.setTextColor(ContextCompat.getColor(context, R.color.errorColor));

10
为什么setTextColor需要使用getResources().getColor()而不是直接使用R.color.errorColor引用? R.color.x在几乎所有其他方法中都可以工作。非常令人沮丧! 为什么setTextColor方法要使用getResources().getColor()而不是直接使用R.color.errorColor引用?虽然R.color.x在几乎所有其他方法中都可以使用,但是这种方法可能需要一个颜色值而不是一个资源ID。这可能是为了与TextView类中的setBackgroundColor方法保持一致,该方法也需要一个颜色值。 - Civilian
9
@Civilian:因为setXXXColor()方法所需的整数参数被视为实际使用的ARGB值,而不是在资源文件中查找的值。奇怪的是,View类具有setBackgroundColor()和setBackgroundResource()方法,而TextView缺少setTextResource()方法。 - Ian Kemp
3
getColor(int)已被弃用。ContextCompat.getColor(getContext(), R.color.yourColor);似乎是替代方案。 - RestInPeace

55

还有一个:

TextView text = (TextView) findViewById(R.id.text);
text.setTextColor(Color.parseColor("#FFFFFF"));

1
getResources() 是一个 Context 成员函数。对于适配器,请使用 getContext().getResources()。颜色值应该放入资源中,就像 @xbakesx 的答案中所示。 - C0D3LIC1OU5

44

你只能通过XML文件来完成这个操作。

在values文件夹中创建一个名为color.xml的文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="textbody">#ffcc33</color>

</resources>

然后在任何XML文件中,都可以使用以下方式设置文本颜色:

android:textColor="@color/textbody"

或者在 Java 文件中使用这种颜色:

final TextView tvchange12 = (TextView) findViewById(R.id.textView2);
//Set color for textbody from color.xml file
tvchange1.setTextColor(getResources().getColor(R.color.textbody));

29

你可以使用

holder.text.setTextColor(Color.rgb(200,0,0));

您还可以使用透明度指定所需的颜色。

holder.text.setTextColor(Color.argb(0,200,0,0));

a代表Alpha(透明度),r代表红色,g代表绿色,b代表蓝色


19

在layout.xml中使用以下代码:

<TextView  android:id="@+id/textView1"    
android:layout_width="wrap_content"    
android:layout_height="wrap_content" 
android:text="@string/add"
android:layout_marginTop="16dp"
android:textAppearance="?
android:attr/textAppearanceMedium"
android:textColor="#25383C"
android:textSize="13sp" />

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:layout_marginTop="16dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#25383C"
        android:textSize="13sp" />

16

有许多不同的方法可以在文本视图上设置颜色。

  1. 将颜色值添加到Studio中的res->values->colors.xml文件中,如下所示:

    <color name="color_purple">#800080</color>
    

    现在您可以在XML或Activity类中设置颜色,如下所示:

    text.setTextColor(getResources().getColor(R.color.color_purple)
    
    如果您想直接使用颜色代码,请使用下面的Color.parseColor代码。
    textView.setTextColor(Color.parseColor("#ffffff"));   
    
  2. 你也可以使用 RGB

  3. text.setTextColor(Color.rgb(200,0,0));
    
  4. 您也可以在textView中使用直接的十六进制代码。您也可以插入纯HEX,如下所示:

  5. text.setTextColor(0xAARRGGBB);
    
  6. 您也可以使用带有Alpha值的ARGB。

  7.    text.setTextColor(Color.argb(0,200,0,0));
    

    a代表Alpha(透明)v。

  8. 如果您正在使用Compat库,可以执行以下操作:

       text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
    

10
textView.setTextColor(ContextCompat.getColor(getApplicationC‌​ontext(),R.color.col‌​orWhite)); 

colors.xml 文件中写入以下代码:

<color name="colorWhite">#FFFFFF</color>

9

我通常对所有的视图都这样做:

myTextView.setTextColor(0xAARRGGBB);

在IT技术中,常用的颜色表示方法是使用16进制码。其中,AA代表透明度(00代表完全透明,FF代表完全不透明),RRGGBB代表HTML标准颜色代码(例如FF0000代表红色)。


为什么要点踩?您能否请提供一些想法? - A.W

7

text.setTextColor(getResource().getColor(R.color.black)) 您需要在color.xml中创建黑色颜色。

或者

text.setTextColor(Color.parseColor("#000000")) 在这里输入所需的十六进制码。

或者

text.setTextColor(Color.BLACK) 您可以使用静态颜色字段。


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