Android Development notes-three ways to change the font color

Source: Internet
Author: User

When adding text in textview, you sometimes need to change the color of some text fonts. Today I learned three ways to share them:

1. Set the font color directly in the configuration XML file under the layout file, and change the color by adding Android: textcolor = "# ffffff"

However, this effect only allows the font to display one color.

2. in the activity, use textview TV = new textview (this); to instantiate a textview, use setcontentview (TV); to load it to the current activity, set the string STR = "content to be displayed ".CodeSome text fonts can be changed. The numbers in the parameters indicate the start position and end position. This method is complicated.

Spannablestringbuilder style = new spannablestringbuilder (STR );
// Spannablestringbuilder implements the 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 TV

3. Use HTML tags to change the text color

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

The HTML tag can be used in the string through the HTML. fromhtml () method, and the font tag can be used to change the font format.

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.