Sometimes a text box to emphasize the content needs to display different colors , with the following code can be easily implemented
Method One: (for the case of many color changes)
Set multiple colors textview= (TextView) Findviewbyid (r.id.text_show) for text boxes; Spannablestringbuilder style = new Spannablestringbuilder ("Remark: Recipient (Zhang San)"); Style.setspan (New Foregroundcolorspan (Color.Blue), 0, 3, spannable.span_exclusive_exclusive); Style.setspan (New Foregroundcolorspan (color.red), 7, 9, spannable.span_exclusive_exclusive); Textview.settext (style);
Method Two: (High school, fast)
The color attribute is set first in the string.
String Str1=string.format ("Price: <font color=\" #d40000 \ ">%s", String.Format ("¥%1$.2f Yuan", Item.getprice ())); String Str2=string.format ("state: <font color=\" #666666 \ ">%s", "sold");
Then use the html.fromhtml () method
Mstaringpricetv.settext (html.fromhtml (str1)); Mcountdowntimetv.settext (html.fromhtml (str2));
Situations like this can be very convenient with a text box .
Android One TextView 2 efficient ways to set multiple colors