When adding text in TextView, sometimes you need to change some text font color, today learned three ways to share 1. Set the font color directly in the configuration XML file under the layout file by adding android:textcolor= "#FFFFFF" To change the color
But this effect only allows the font to display a color
2. In activity through TextView tv=new TextView (this); instantiate a TextView, via Setcontentview (TV), load it into the current activity, set the content to be displayed string Str= "What you want to show"; The following code can be used to achieve some changes in the text font, the number in the parameter represents the starting position and the end position, this method is more complex
Spannablestringbuilder style=New//style.setspan (new Foregroundcolorspan (color.red), 0, 2, spannable.span_exclusive_exclusive); Style.setspan (new Foregroundcolorspan (Color.yellow), 2, 4, spannable.span_exclusive_exclusive); Style.setspan (new Foregroundcolorspan (Color.green), 4, 6, spannable.span_exclusive_exclusive); Tv.settext (style); //
3. Change text color with HTML tags
By using the html.fromhtml () method, you can use HTML tags in strings, and font tags can be used to change the format of the fonts.
Three ways Android changes font color