Android Development Change Font Color method _android

Source: Internet
Author: User

When you add text to TextView, you sometimes change the color of some text fonts, and today you are sharing three implementations and the pros and cons.

1. Change text color through HTML tags

Copy Code code as follows:

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

Comment: Using the html.fromhtml () method, you can use HTML tags in a string, and font labels can be used to change the format of fonts. Wheat College-the most professional it online education platform in China.

2, in the activity through TextView tv=new TextView (this) , instantiate a TextView, through the Setcontentview (TV), load it into the current activity, Set the content to display string str= "What you want to display";

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

Comments: Through the above code can be implemented part of the text font changes, the number of parameters to indicate the starting position and end position, this method is more complex

3. Set the font color directly in the configuration XML file under Layout file , and change the color by adding android:textcolor= "#FFFFFF"

Comment on: Only allow fonts to display a color

The above mentioned is the entire content of this article, I hope you can enjoy.

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.