Android develops three ways to change the font color of notes
- 2020-05-05 11:51:04
- OfStack
1. Directly set the font color in the configuration xml file under layout, and change the color
by adding android: textcolor= "#FFFFFF"
But this effect will only make the font uniform display of one color
2. TextView tv=new TextView(this) in activity; Instantiate an textview by setContentView(tv); Load it into the current activity and set the content to be displayed to String str= "the content to be displayed"; Part of the text font can be changed with the following code. The Numbers in the parameters represent the starting and ending positions. This method is more complicated
3. Change the text color by html tag
The Html.fromHtml () method lets you use the html tag in a string, and the font tag lets you change the font format
by adding android: textcolor= "#FFFFFF"
But this effect will only make the font uniform display of one color
2. TextView tv=new TextView(this) in activity; Instantiate an textview by setContentView(tv); Load it into the current activity and set the content to be displayed to String str= "the content to be displayed"; Part of the text font can be changed with the following code. The Numbers in the parameters represent the starting and ending positions. This method is more complicated
SpannableStringBuilder style=new SpannableStringBuilder(str);
//SpannableStringBuilder implementation 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 In the
3. Change the text color by html tag
tv.setText(Html.fromHtml(" I am a <font color=blue>danyijiangnan</font>"));
The Html.fromHtml () method lets you use the html tag in a string, and the font tag lets you change the font format