When you add text to TextView, you sometimes change the color of some text fonts, and today you are sharing three implementations and the pros and cons.
1. Change text color through HTML tags
Copy Code code as follows:
Tv.settext (html.fromhtml ("I am <font color=blue>danyijiangnan</font>"));
Comment: Using the html.fromhtml () method, you can use HTML tags in a string, and font labels can be used to change the format of fonts. Wheat College-the most professional it online education platform in China.
2, in the activity through TextView tv=new TextView (this) , instantiate a TextView, through the Setcontentview (TV), load it into the current activity, Set the content to display string str= "What you want to display";
Copy Code code as follows:
Spannablestringbuilder style=new Spannablestringbuilder (str);
Spannablestringbuilder Implement 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 the TV
Comments: Through the above code can be implemented part of the text font changes, the number of parameters to indicate the starting position and end position, this method is more complex
3. Set the font color directly in the configuration XML file under Layout file , and change the color by adding android:textcolor= "#FFFFFF"
Comment on: Only allow fonts to display a color
The above mentioned is the entire content of this article, I hope you can enjoy.