ChatGPT解决这个技术问题 Extra ChatGPT

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

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

我试过类似的东西:

holder.text.setTextColor(R.color.Red);

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

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

关于在代码中调整 UI 的说明,请考虑在设计时查看 UI 的优势,将运行时更改降至最低。

m
magnussn

你应该使用:

holder.text.setTextColor(Color.RED);

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

Color.parseColor (Manual) (就像 LEX 使用的) text.setTextColor(Color.parseColor("#FFFFFF"));

Color.rgb 和 Color.argb (Manual rgb) (Manual argb) (就像 Ganapathy 使用的那样) holder.text.setTextColor(Color.rgb(200,0,0)); holder.text.setTextColor(Color.argb(0,200,0,0));

当然,如果你想在 XML 文件中定义你的颜色,你可以这样做: #f00 因为 getColor() 函数已被弃用1,你需要像这样使用它所以: ContextCompat.getColor(context, R.color.your_color);

您还可以插入普通 HEX,如下所示: myTextView.setTextColor(0xAARRGGBB);首先是 alpha 通道,然后是颜色值。

当然,请查看完整的手册,public class Color extends Object

1此代码也曾在这里:

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

此方法现在在 Android M 中已弃用。不过,您可以从 contextCompat in the support library 中使用它,如现在的示例所示。


另外,如果文本是链接,则需要使用 text.setLinkTextColor(...);在代码中或 android:textColorLink="..." 在 XML
@Nanne 如果您的回答还提到 R.color.XXX 是对颜色的引用,那就太好了。这意味着为了清楚起见,它需要被取消引用(就像在你的例子中一样)。
我不确定你是什么意思?如中,取消引用等将使用更多资源还是您的意思是别的?
getColor(int) 已弃用。
如何导入颜色类?
x
xbakesx

如果您仍想在 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));

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

还有一个:

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

getResources() 是一个上下文成员函数。对于适配器,使用 getContext().getResources()。颜色值应该进入资源,就像@xbakesx 的回答一样。
P
Peter Mortensen

您也只能从 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));

b
bkaid

您可以使用

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

您还可以使用透明度指定您想要的颜色。

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

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


A
Ankur Bhadania

在 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" />

S
Suragch

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

在工作室 res->values->colors.xml 中添加颜色值作为 #800080 现在将 xml 或 actvity 类中的颜色设置为 text.setTextColor(getResources().getColor(R .color.color_purple) 如果要直接给出颜色代码,请在下面使用 Color.parseColor 代码 textView.setTextColor(Color.parseColor("#ffffff")); 也可以使用 RGB text.setTextColor(Color.rgb(200,0 ,0)); 使用也可以直接使用十六进制代码进行 textView。您也可以插入普通 HEX,如下所示: text.setTextColor(0xAARRGGBB); 您还可以使用带有 alpha 值的 argb。text.setTextColor(Color.argb(0,200, 0,0)); a for Alpha (Transparent) v. 如果您使用的是 Compat 库,您可以执行以下操作 text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));


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

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

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

P
Peter Mortensen

我通常对任何视图都这样做:

myTextView.setTextColor(0xAARRGGBB);

在哪里

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

RRGGBB 定义了正常的 HTML 颜色代码(如红色的 FF0000)。


为什么投反对票?你能补充一些想法吗?
R
Robin Gawenda

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

这有效:

textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);
textView.setTextColor(Color.RED);

虽然这会导致您的文本颜色为例如白色(对于深色主题)或黑色(对于浅色主题):

textView.setTextColor(Color.RED);
textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);

与 XML 中的相反,顺序是任意的。


P
Peter Mortensen

我相信,如果您想将颜色指定为资源(在 XML 文件中),则必须提供其 ARGB 值(而不仅仅是 RGB 值)。

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


根据我的经验,这不是真的,可以使用 RGB 值而不是 ARGB 值
R
Ronnie Oosting

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

或者

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

或者

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


P
Peter Mortensen

利用:

TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(285,0,0));

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

或者

myTextView.setTextColor(0xAARRGGBB);

G
Gibolt

Kotlin 扩展解决方案

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

用于设置 ColorInt

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

var TextView.textColor: Int
get() = currentTextColor
set(@ColorInt color) {
    setTextColor(color)
}

用于设置 ColorRes

myView.setTextColorRes(R.color.my_color)

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

P
Peter Mortensen

使用适配器,您可以使用以下代码设置文本颜色:

holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
holder.text_view.setTextColor(Color.parseColor("#FF00FF"));

k
karanatwal.github.io
TextView text = new TextView(context);
text.setTextColor(Color.parseColor("any hex value of a color"));

上面的代码在我这边工作。这里的 text 是一个需要设置颜色的 TextView


P
Pang
text1.setTextColor(Color.parseColor("#000000"));

M
Mori

如果你使用 Kotlin,有 4 种方式:(使用 Holder)

使用 Android 资源:holder.textView.setTextColor(Color.GREEN) 使用 RGB:holder.textView.setTextColor(Color.rgb(255, 87, 34))

3)使用十六进制:

holder.textView.setTextColor(Color.parseColor("#C2185B"))

4)使用项目资源:(需要API级别23)

holder.textView.setTextColor(context.resources.getColor(R.color.colorMax,null))

J
JanS

从 API 23 开始,getResources().getColor() 已弃用。

改用这个:

textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.color_black));

z
zudo1337
   textViewStatus.setTextColor(res.getColor(R.color.green));

P
Peter Mortensen

在适配器中,您可以使用以下代码设置文本颜色:

holder.my_text_view = (TextView) convertView.findViewById(R.id.my_text_view);
holder.my_text_view.setTextColor(Color.parseColor("#FFFFFF"));

r
reshma

如果你想直接给出颜色代码然后使用

textView.setTextColor(Color.parseColor("#ffffff"));

或者,如果您想从颜色文件夹中提供颜色代码,请使用

textView.setTextColor(R.color.white);

此代码 textView.setTextColor(R.color.white);不起作用。您可以使用 text.setTextColor(getResources().getColor(R.color.color_purple) 从您的 color.xml 中获取颜色
C
Cristiano Guerra

我是这样做的:在 res/values 文件夹中创建一个名为 Colors 的 XML 文件。

我的颜色.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="vermelho_debito">#cc0000</color>
    <color name="azul_credito">#4c4cff</color>
    <color name="preto_bloqueado">#000000</color>
    <color name="verde_claro_fundo_lista">#CFDBC5</color>
    <color name="branco">#ffffff</color>
    <color name="amarelo_corrige">#cccc00</color>
    <color name="verde_confirma">#66b266</color>
</resources>

为了从 xml 文件中获取这些颜色,我使用了以下代码: valor 它是一个 TextView,而 ctx 它是一个 Context 对象。我不是从 Activity 中使用它,而是从 BaseAdapter 到 ListView。这就是我使用这个上下文对象的原因。

valor.setTextColor(ctx.getResources().getColor(R.color.azul_credito));

希望能帮助到你。


I
IntelliJ Amiya

为了设置 TextView 的颜色,TextView.setTextColor(R.color.YOURCOLOR) 是不够的!

它必须像这样使用 -

TextView myText = (TextView) findViewById(R.id.YoutTextViewID);

myText.setTextColor(getResources().getColor(R.color.YOURCOLOR);

或者

myText.setTextColor(Color.parseColor("#54D66A"));

S
Soner Gönül
holder.userType.setTextColor(context.getResources().getColor(
                    R.color.green));

m
mohamad sheikhi

尝试这个:

TextView textview = (TextView) findViewById(R.id.textview );
textview .setTextColor(Color.parseColor("#85F85F"));

P
Peter Mortensen

同样,我使用的是 color.xml

<color name="white">#ffffff</color>
    <color name="black">#000000</color>   

用于设置 TextView 背景,如:

textView.setTextColor(R.color.white);

我得到了不同的颜色,但是当我使用下面的代码时,我得到了实际的颜色。

textView.setTextColor(Color.parseColor("#ff6363"));

M
Moak

用于提供 rgb 值:text.setTextColor(Color.rgb(200,0,0));
用于从十六进制值解析颜色:text.setTextColor(Color.parseColor("#FFFFFF"));


A
Aleks Nine

如果您在适配器中并且仍想使用资源中定义的颜色,您可以尝试以下方法:

holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.myRed));