Android One TextView 2 efficient ways to set multiple colors

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.