In real-world applications, you need to display a string with a different color and font. Of course, it can be stitched together by different textview. Can also be shown through a textview.
The steps are as follows:
1. Define different style.
You might want to define 2 style as follows
<style name= "Style0" > <item name= "android:textsize" >19dip</item> <item name= "Android : TextColor "> @color/color1</item> </style><style name=" Style1 "> <item name=" Android:textsize ">23dip</item> <item name=" Android:textcolor "> @color/color2</item> <item name= "Android:textstyle" >italic</item></style>
2. Format the string with spannablestring. The code is as follows:
Mtextview = (TextView) Findviewbyid (r.id.test); spannablestring styledtext = new Spannablestring ("Android, hello"); Styledtext.setspan (New Textappearancespan (this, R.STYLE.STYLE0), 0, 3, spanned.span_exclusive_exclusive); Styledtext.setspan (New Textappearancespan (this, r.style.style1), 3, 5, spanned.span_exclusive_exclusive); Mtextview.settext (Styledtext, TextView.BufferType.SPANNABLE);
Reference: http://blog.csdn.net/fengyoujie/article/details/42783891
Android TextView set different font sizes and colors