When adding text in textview, you sometimes need to change the color of some text fonts. Today I learned three ways to share them:
1. Set the font color directly in the configuration XML file under the layout file, and change the color by adding Android: textcolor = "# ffffff"
However, this effect only allows the font to display one color.
2. in the activity, use textview TV = new textview (this); to instantiate a textview, use setcontentview (TV); to load it to the current activity, set the string STR = "content to be displayed ".CodeSome text fonts can be changed. The numbers in the parameters indicate the start position and end position. This method is complicated.
Spannablestringbuilder style = new spannablestringbuilder (STR );
// Spannablestringbuilder implements the charsequence Interface
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); // Add it to TV
3. Use HTML tags to change the text color
TV. settext (html. fromhtml ("I am <font color = blue> danyijiangnan </font> "));
The HTML tag can be used in the string through the HTML. fromhtml () method, and the font tag can be used to change the font format.