Android Development Notes three ways to change font color _android

Source: Internet
Author: User
Tags html tags
1. Set the font color directly in the configuration XML file in the layout file to change the color by adding android:textcolor= "#FFFFFF"

But this effect only allows the font to display a color

2. In the activity by TextView Tv=new TextView (this); Instantiate a TextView through Setcontentview (TV); load it into the current activity, set the content string to display Str= "What you want to show"; The following code can be used to change the font of some text, the number in the parameter indicates the starting position and the end position, this method is more complicated
Copy Code code as follows:

Spannablestringbuilder style=new Spannablestringbuilder (str);
Spannablestringbuilder Implement 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 the TV

3. Change text color through HTML tags
Copy Code code as follows:

Tv.settext (html.fromhtml ("I am <font color=blue>danyijiangnan</font>"));

The Html.fromhtml () method allows you to use HTML tags in a string that can change the format of a font
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.