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.rgb
和Color.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中使用它,如示例所示。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…