在 XML 中,我们可以通过 textColor
属性设置文本颜色,例如 android:textColor="#FF0000"
。但是我如何通过编码来改变它?
我试过类似的东西:
holder.text.setTextColor(R.color.Red);
其中 holder
只是一个类,而 text
是 TextView
类型。红色是在字符串中设置的 RGB 值 (#FF0000)。
但它显示不同的颜色而不是红色。我们可以在 setTextColor() 中传递什么样的参数?在文档中,它说 int
,但它是资源参考值还是其他什么?
你应该使用:
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 文件中定义你的颜色,你可以这样做:
您还可以插入普通 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 中使用它,如现在的示例所示。
如果您仍想在 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));
getColor(int)
已弃用。 ContextCompat.getColor(getContext(), R.color.yourColor);
似乎是替代品。
还有一个:
TextView text = (TextView) findViewById(R.id.text);
text.setTextColor(Color.parseColor("#FFFFFF"));
getResources()
是一个上下文成员函数。对于适配器,使用 getContext().getResources()
。颜色值应该进入资源,就像@xbakesx 的回答一样。
您也只能从 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));
您可以使用
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
在 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" />
有许多不同的方法可以在文本视图上设置颜色。
在工作室 res->values->colors.xml 中添加颜色值作为
textView.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.colorWhite));
在 colors.xml
文件中,写入以下代码:
<color name="colorWhite">#FFFFFF</color>
我通常对任何视图都这样做:
myTextView.setTextColor(0xAARRGGBB);
在哪里
AA 定义 alpha(00 表示透明,FF 表示不透明)
RRGGBB 定义了正常的 HTML 颜色代码(如红色的 FF0000)。
如果您打算使用 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 中的相反,顺序是任意的。
text.setTextColor(getResource().getColor(R.color.black))
您在 color.xml 中创建了黑色。
或者
text.setTextColor(Color.parseColor("#000000"))
在此处输入所需的十六进制代码
或者
text.setTextColor(Color.BLACK)
您可以使用静态颜色字段
利用:
TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(285,0,0));
holder.text.setTextColor(Color.rgb(200,0,0));
或者
myTextView.setTextColor(0xAARRGGBB);
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)
}
使用适配器,您可以使用以下代码设置文本颜色:
holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
holder.text_view.setTextColor(Color.parseColor("#FF00FF"));
TextView text = new TextView(context);
text.setTextColor(Color.parseColor("any hex value of a color"));
上面的代码在我这边工作。这里的 text
是一个需要设置颜色的 TextView。
如果你使用 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))
从 API 23 开始,getResources().getColor()
已弃用。
改用这个:
textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.color_black));
textViewStatus.setTextColor(res.getColor(R.color.green));
在适配器中,您可以使用以下代码设置文本颜色:
holder.my_text_view = (TextView) convertView.findViewById(R.id.my_text_view);
holder.my_text_view.setTextColor(Color.parseColor("#FFFFFF"));
如果你想直接给出颜色代码然后使用
textView.setTextColor(Color.parseColor("#ffffff"));
或者,如果您想从颜色文件夹中提供颜色代码,请使用
textView.setTextColor(R.color.white);
我是这样做的:在 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));
希望能帮助到你。
为了设置 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"));
holder.userType.setTextColor(context.getResources().getColor(
R.color.green));
尝试这个:
TextView textview = (TextView) findViewById(R.id.textview );
textview .setTextColor(Color.parseColor("#85F85F"));
同样,我使用的是 color.xml
:
<color name="white">#ffffff</color>
<color name="black">#000000</color>
用于设置 TextView
背景,如:
textView.setTextColor(R.color.white);
我得到了不同的颜色,但是当我使用下面的代码时,我得到了实际的颜色。
textView.setTextColor(Color.parseColor("#ff6363"));
用于提供 rgb 值:text.setTextColor(Color.rgb(200,0,0));
用于从十六进制值解析颜色:text.setTextColor(Color.parseColor("#FFFFFF"));
如果您在适配器中并且仍想使用资源中定义的颜色,您可以尝试以下方法:
holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.myRed));
不定期副业成功案例分享
getColor(int)
已弃用。