Three methods for changing the font color of Android Development notes

Source: Internet
Author: User

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 content to be displayed String str = "content to be displayed"; use the following code to change some text fonts. The numbers in the parameters indicate the start position and end position. This method is complex.Copy codeThe Code is as follows: 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 colorCopy codeThe Code is as follows: 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.