Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

android - 如何在代码中设置TextView的文本颜色?(How to set the text color of TextView in code?)

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .

(在XML中,我们可以通过textColor属性设置文本颜色,例如android:textColor="#FF0000" 。)

But how do I change it by coding?

(但是,如何通过编码进行更改?)

I tried something like:

(我尝试了类似的东西:)

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

Where holder is just a class and text is of type TextView .

(其中holder是一个类, textTextView类型。)

Red is an RGB value (#FF0000) set in strings.

(红色是在字符串中设置的RGB值(#FF0000)。)

But it shows a different color rather than red.

(但是它显示出不同的颜色,而不是红色。)

What kind of parameter can we pass in setTextColor()?

(我们可以在setTextColor()中传递什么样的参数?)

In documentation, it says int , but is it a resource reference value or anything else?

(在文档中,它表示为int ,但这是资源参考值还是其他值?)

  ask by Vikas translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use:

(您应该使用:)

holder.text.setTextColor(Color.RED);

For a sanity check, I just tried it, because I had a project open anyway, and yes, it's nice and red ;D

(对于完整性检查,我只是尝试了一下,因为无论如何我都有一个打开的项目,是的,它很好看而且很红; D)


You can use various functions from the Color class to get the same effect of course.

(您可以使用Color类中的各种功能来获得相同的效果。)

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

    (Color.parseColor (手动) (例如LEX使用))

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

    (Color.rgbColor.argb手动rgb )( 手动argb )(像Ganapathy一样))

     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:

    (当然,如果要在XML文件中定义颜色,则可以执行以下操作:)

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

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

    (因为不建议使用getColor()函数1 ,所以您需要像这样使用它:)

     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.

    (首先有一个Alpha通道的地方,然后是颜色值。)

Check out the complete manual of course, public class Color extends Object .

(当然要查看完整的手册, 公共类Color扩展Object 。)


1 This code used to be in here as well:

(1此代码也曾经在这里:)

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

This method is now deprecated in Android M. You can however use it from the contextCompat in the support library , as the example now shows.

(现在已在Android M中弃用了该方法。但是,您可以从支持库contextCompat中使用它,如示例所示。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...