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

Vik*_*kas 520 android colors textview

在XML中,我们可以通过textColor属性设置文本颜色,例如android:textColor="#FF0000".但是如何通过编码来改变它呢?

我尝试过类似的东西:

holder.text.setTextColor(R.color.Red);
Run Code Online (Sandbox Code Playgroud)

哪里holder只是一个类,text是类型TextView.红色是以字符串形式设置的RGB值(#FF0000).

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

Nan*_*nne 1190

你应该使用:

holder.text.setTextColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)

为了进行健全性检查,我只是试了一下,因为无论如何我打开了一个项目,是的,这很好看并且很红; D


您可以使用Color课程中的各种功能来获得相同的效果.

查看完整的手册当然是公共类Color extends Object.


1这段代码也曾在这里:

textView.setTextColor(getResources().getColor(R.color.errorColor));
Run Code Online (Sandbox Code Playgroud)

现在,在Android M中不推荐使用此方法.但是,您可以从支持库中contextCompat使用它,如现在的示例所示.

  • 不推荐使用`getColor(int)`. (5认同)
  • 另外,如果文本是链接,则需要使用 text.setLinkTextColor(...); 在代码或 android:textColorLink="..." 在 XML (2认同)

xba*_*esx 136

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

<color name="errorColor">#f00</color>
Run Code Online (Sandbox Code Playgroud)

然后使用以下两种方法之一在代码中引用它:

textView.setTextColor(getResources().getColor(R.color.errorColor, getResources().newTheme()));    
Run Code Online (Sandbox Code Playgroud)

要么

textView.setTextColor(getResources().getColor(R.color.errorColor, null));
Run Code Online (Sandbox Code Playgroud)

如果您正在针对Android M进行编译,那么第一个可能更好,但是您传入的主题可能为空,所以也许这对您来说更容易?

如果你正在使用Compat库,你可以做这样的事情

textView.setTextColor(ContextCompat.getColor(context, R.color.errorColor));
Run Code Online (Sandbox Code Playgroud)

  • 对于setTextColor,为什么它必须采用getResources().getColor()而不是直接的R.color.errorColor引用?R.color.x适用于几乎所有其他方法.令人难以置信的沮丧! (9认同)
  • @Civilian:因为setXXXColor()方法所需的int参数被视为要使用的实际ARGB值,而不是要在资源文件中查找的值.奇怪的是,View类同时具有setBackgroundColor()和setBackgroundResource(),而TextView缺少setTextResource()方法. (8认同)
  • `getColor(int)` 已弃用。`ContextCompat.getColor(getContext(), R.color.yourColor);` 似乎是替代品。 (2认同)

小智 45

还有一个:

TextView text = (TextView) findViewById(R.id.text);
text.setTextColor(Color.parseColor("#FFFFFF"));
Run Code Online (Sandbox Code Playgroud)


new*_*day 37

您也只能从XML文件中执行此操作.

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

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

</resources>
Run Code Online (Sandbox Code Playgroud)

然后在任何XML文件中,您可以使用设置文本的颜色,

android:textColor="@color/textbody"
Run Code Online (Sandbox Code Playgroud)

或者您可以在Java文件中使用此颜色:

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


Gan*_*y C 27

您可以使用

holder.text.setTextColor(Color.rgb(200,0,0));
Run Code Online (Sandbox Code Playgroud)

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

holder.text.setTextColor(Color.argb(0,200,0,0));
Run Code Online (Sandbox Code Playgroud)

a表示Alpha(透明)值r-red g-green b-blue


Ran*_*ank 15

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

  1. 在studio res-> values-> colors.xml中添加颜色值为

    <color name="color_purple">#800080</color>
    
    Run Code Online (Sandbox Code Playgroud)

    现在将xml或actvity类中的颜色设置为

    text.setTextColor(getResources().getColor(R.color.color_purple)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果要直接使用Color.parseColor代码,请使用颜色代码

    textView.setTextColor(Color.parseColor("#ffffff"));   
    
    Run Code Online (Sandbox Code Playgroud)
  3. 您也可以使用RGB

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

    text.setTextColor(0xAARRGGBB);
    
    Run Code Online (Sandbox Code Playgroud)
  5. 您还可以将argb与alpha值一起使用.

       text.setTextColor(Color.argb(0,200,0,0));
    
    Run Code Online (Sandbox Code Playgroud)

    a for Alpha(透明)v.

  6. 如果你正在使用Compat库,你可以做这样的事情

       text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
    
    Run Code Online (Sandbox Code Playgroud)


小智 14

在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" />
Run Code Online (Sandbox Code Playgroud)


A.W*_*A.W 9

我通常会对任何观点执行此操作:

myTextView.setTextColor(0xAARRGGBB);
Run Code Online (Sandbox Code Playgroud)

哪里

  • AA定义alpha(00表示透明,FF表示不透明)

  • RRGGBB定义了普通的HTML颜色代码(如红色的FF0000).


Rob*_*nda 7

如果您打算使用setTextAppearance,您应该知道它将使用从主题继承的样式覆盖文本颜色.因此,如果要同时使用两者,请在之后设置颜色.

这有效:

textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);
textView.setTextColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)

虽然这会导致你的textcolor例如是白色(对于黑暗主题)或黑色(对于浅色主题):

textView.setTextColor(Color.RED);
textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);
Run Code Online (Sandbox Code Playgroud)

与XML相反,顺序是任意的.


Arn*_*rty 6

我相信如果你想指定一种颜色作为资源(在XML文件中),你必须提供它的ARGB值(而不仅仅是RGB值).

尝试将颜色值更改为#FFFF0000.它应该给你RED.


小智 6

textView.setTextColor(ContextCompat.getColor(getApplicationC??ontext(),R.color.col??orWhite)); 
Run Code Online (Sandbox Code Playgroud)

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

<color name="colorWhite">#FFFFFF</color>
Run Code Online (Sandbox Code Playgroud)


Tal*_*lha 5

使用:

TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(285,0,0));
Run Code Online (Sandbox Code Playgroud)


小智 5

holder.text.setTextColor(Color.rgb(200,0,0));
Run Code Online (Sandbox Code Playgroud)

要么

myTextView.setTextColor(0xAARRGGBB);
Run Code Online (Sandbox Code Playgroud)


Nit*_*ish 5

text.setTextColor(getResource().getColor(R.color.black)) 您已经在color.xml中创建了黑色。

要么

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

要么

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


Gib*_*olt 5

Kotlin 扩展解决方案

添加这些以使更改文本颜色更简单

用于设置 ColorInt

myView.textColor = Color.BLACK // or Color.parseColor("#000000"), etc.

var TextView.textColor: Int
get() = currentTextColor
set(@ColorInt color) {
    setTextColor(color)
}
Run Code Online (Sandbox Code Playgroud)

用于设置 ColorRes

myView.setTextColorRes(R.color.my_color)

fun TextView.setTextColorRes(@ColorRes colorRes: Int) {
    val color = ContextCompat.getColor(context, colorRes)
    setTextColor(color)
}
Run Code Online (Sandbox Code Playgroud)