在XML中,我们可以按textColor
属性设置文字颜色,例如android:textColor="#FF0000"
。但是如何通过编码来改变它?
我尝试过类似的事情:
holder.text.setTextColor(R.color.Red);
其中holder
只是一个类,而text
的类型为TextView
。红色是以字符串形式设置的RGB值(#FF0000)。
但它显示的是不同颜色而不是红色。我们可以在setTextColor()中传递什么样的参数?在文档中,它显示int
,但它是资源引用值还是其他任何内容?
答案 0 :(得分:1150)
您应该使用:
holder.text.setTextColor(Color.RED);
为了进行健全性检查,我只是尝试过,因为无论如何我打开了一个项目,是的,这很好看并且很红; D
您可以使用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
文件中定义颜色,可以这样做:
<color name="errorColor">#f00</color>
因为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中的方法,如下所示。
答案 1 :(得分:133)
如果您仍想在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));
答案 2 :(得分:44)
还有一个:
TextView text = (TextView) findViewById(R.id.text);
text.setTextColor(Color.parseColor("#FFFFFF"));
答案 3 :(得分:36)
您也只能从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));
答案 4 :(得分:25)
您可以使用
holder.text.setTextColor(Color.rgb(200,0,0));
您还可以使用透明度指定所需的颜色。
holder.text.setTextColor(Color.argb(0,200,0,0));
a for Alpha (透明)值r-red g-green b-blue
答案 5 :(得分:14)
在文本视图上设置颜色的方法有很多种。
在studio res-&gt; values-&gt; colors.xml中添加颜色值为
<color name="color_purple">#800080</color>
现在将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);
您还可以将argb与alpha值一起使用。
text.setTextColor(Color.argb(0,200,0,0));
a for Alpha(Transparent)v。
如果你正在使用Compat库,你可以做这样的事情
text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
答案 6 :(得分:13)
在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" />
答案 7 :(得分:8)
我通常会针对任何观点执行此操作:
myTextView.setTextColor(0xAARRGGBB);
其中
AA定义alpha(00表示透明,FF表示不透明)
RRGGBB定义了普通的HTML颜色代码(如红色的FF0000)。
答案 8 :(得分:7)
如果您打算使用 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中的相反,顺序是任意的。
答案 9 :(得分:5)
答案 10 :(得分:4)
textView.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.colorWhite));
在colors.xml
文件中,写下面的代码:
<color name="colorWhite">#FFFFFF</color>
答案 11 :(得分:4)
holder.text.setTextColor(Color.rgb(200,0,0));
或
myTextView.setTextColor(0xAARRGGBB);
答案 12 :(得分:4)
使用:
TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(285,0,0));
答案 13 :(得分:3)
TRUE
以上代码正在我身边。此处TextView text = new TextView(context);
text.setTextColor(Color.parseColor("any hex value of a color"));
是 TextView ,需要在其上设置颜色。
答案 14 :(得分:3)
text1.setTextColor(Color.parseColor("#000000"));
答案 15 :(得分:3)
text.setTextColor(getResource().getColor(R.color.black))
您已经在color.xml中创建了黑色。
OR
text.setTextColor(Color.parseColor("#000000"))
在此处键入所需的十六进制代码
OR
text.setTextColor(Color.BLACK)
您可以使用静态色域
答案 16 :(得分:3)
使用适配器,您可以使用以下代码设置文本颜色:
holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
holder.text_view.setTextColor(Color.parseColor("#FF00FF"));
答案 17 :(得分:2)
添加这些内容可以使更改文字颜色更加简单
myView.textColor = Color.BLACK // or Color.parseColor("#000000"), etc.
var TextView.textColor: Int
get() = currentTextColor
set(@ColorInt color) {
setTextColor(color)
}
myView.setTextColorRes(R.color.my_color)
fun TextView.setTextColorRes(@ColorRes colorRes: Int) {
val color = ContextCompat.getColor(context, colorRes)
setTextColor(color)
}
答案 18 :(得分:1)
为了设置TextView的颜色,TextView.setTextColor(R.color.YOURCOLOR)
还不够!
必须像这样使用 -
TextView myText = (TextView) findViewById(R.id.YoutTextViewID);
myText.setTextColor(getResources().getColor(R.color.YOURCOLOR);
OR
myText.setTextColor(Color.parseColor("#54D66A"));
答案 19 :(得分:1)
如果您想直接提供颜色代码,请使用
textView.setTextColor(Color.parseColor("#ffffff"));
或者如果您想从colors文件夹中提供颜色代码,请使用
textView.setTextColor(R.color.white);
答案 20 :(得分:1)
textViewStatus.setTextColor(res.getColor(R.color.green));
答案 21 :(得分:1)
holder.userType.setTextColor(context.getResources().getColor(
R.color.green));
答案 22 :(得分:1)
在适配器中,您可以使用以下代码设置文本颜色:
holder.my_text_view = (TextView) convertView.findViewById(R.id.my_text_view);
holder.my_text_view.setTextColor(Color.parseColor("#FFFFFF"));
答案 23 :(得分:1)
从API 23开始,不推荐使用request()->url()
。
请改用:
getResources().getColor()
答案 24 :(得分:0)
尝试一下:
TextView textview = (TextView) findViewById(R.id.textview );
textview .setTextColor(Color.parseColor("#85F85F"));
答案 25 :(得分:0)
TextView color= (TextView)findViewById(R.id.color);
text.setTextColor(Color.RED);
答案 26 :(得分:0)
尝试一下:
textView.setTextColor(getResources().getColor(R.color.errorColor, null));
答案 27 :(得分:0)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
b.numberDay1.setTextColor(ContextCompat.getColor(requireContext(), R.color.secondary_100))
} else {
b.numberDay1.setTextColor(resources.getColor(R.color.secondary_100))
}
答案 28 :(得分:0)
我在ViewHolder中为RecyclerView执行此操作。我不太清楚为什么,但它在ViewHolder初始化中对我没用。
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
textView.setTextColor(context.getResources().getColor(R.color.myColor));
// Other stuff
}
但是当我将它移动到onBindViewHolder时,它工作正常。
public void onBindViewHolder(ViewHolder holder, int position){
// Other stuff
holder.textView.setTextColor(context.getResources().getColor(R.color.myColor));
}
希望这有助于某人。
答案 29 :(得分:0)
getColor()被取消
所以试试这个:
tv_title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sf_white));
答案 30 :(得分:0)
TextView textresult = (TextView)findViewById(R.id.textView1);
textresult.setTextColor(Color.GREEN);
答案 31 :(得分:0)
如果您在适配器中并且仍想使用资源中定义的颜色,则可以尝试以下方法:
holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.myRed));
答案 32 :(得分:0)
我是这样做的: 创建一个XML文件,在res / values文件夹中称为Colors。
我的Colors.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文件中获取此颜色,我已使用此代码: 勇敢的它是一个TextView,而ctx它是一个Context对象。我没有在Activity中使用它,而是在ListView中使用BaseAdapter。这就是我使用这个上下文对象的原因。
valor.setTextColor(ctx.getResources().getColor(R.color.azul_credito));
希望它有所帮助。
答案 33 :(得分:0)
您可以使用textView.setTextColor(Color.BLACK)
来使用Color
类的任何内置颜色。
您还可以使用textView.setTextColor(Color.parseColor(hexRGBvalue))
来定义自定义颜色。
答案 34 :(得分:0)
提供rgb值:text.setTextColor(Color.rgb(200,0,0));
要从十六进制值解析颜色:
text.setTextColor(Color.parseColor("#FFFFFF"));
答案 35 :(得分:0)
同样,我使用color.xml
:
<color name="white">#ffffff</color>
<color name="black">#000000</color>
用于设置TextView
背景,如:
textView.setTextColor(R.color.white);
我得到了不同的颜色,但是当我使用下面的代码时,我得到了实际的颜色。
textView.setTextColor(Color.parseColor("#ff6363"));
答案 36 :(得分:-1)
尝试使用以下代码:
holder.text.setTextColor(Color.parseColor("F00"));