Three ways Android changes font color

Source: Internet
Author: User

When adding text in TextView, sometimes you need to change some text font color, today learned three ways to share 1. Set the font color directly in the configuration XML file under the layout file by adding android:textcolor= "#FFFFFF" To change the color

But this effect only allows the font to display a color

2. In activity through TextView tv=new TextView (this); instantiate a TextView, via Setcontentview (TV), load it into the current activity, set the content to be displayed string Str= "What you want to show"; The following code can be used to achieve some changes in the text font, the number in the parameter represents the starting position and the end position, this method is more complex
Spannablestringbuilder style=New//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); //


3. Change text color with HTML tags


By using the html.fromhtml () method, you can use HTML tags in strings, and font tags can be used to change the format of the fonts.

Three ways Android changes font color

Related Article

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.