Can I change the color of TextView's text with the code?

Asked 1 years ago, Updated 1 years ago, 134 views

When you change the color of TextView in XML, you change the textColor property like android:textColor="#FF0000"

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

In the code above, holder is a class and text is a TextView. Red is strings with RGB value of #FF0000.

But if you look at the results, the color is a bit different from red. According to the related document, the int is delivered to the setTextColor, but is that only the reference value of the resource? Or something else?

android textview color

2022-09-22 10:58

1 Answers

holder.text.setTextColor(Color.RED);

I think you should change it like this. The red color comes out well when I do it like the chords above.

There are many methods in the Color class that have the same effect.

Color.parseColor text.setTextColor(Color.parseColor("#FFFFFF"));

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

And you can define the color directly in xml. <color name="color name">#f00</color>

GetResources().getColor() is defined from Android M ContextCompat.getColor(context, R.color.Color Name); You can use it like this.

Lastly,

AA represents alpha and RRGGBB represents RGB values respectively.


2022-09-22 10:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.