Android TextView also changes the color and size of some fonts in a string

Source: Internet
Author: User

Android TextView There are two main ways of changing fonts:

1. Html.fromhtml gets a spanned that is represented in Html format.

2, another way is directly constructed using a spanned.

To change the color and size of some fonts in a string at the same time, in the first way, html.fromhtml, with the font tag, unfortunately, the properties on the font in Android only support color and face, but size is not supported. Then completely using the font tag HTML, is not implemented.

If you use HTML to change the font size, you can try a label like <small> or


The second approach is simple and relatively easy to implement, as shown in the following example:

        Colorstatelist redcolors = colorstatelist.valueof (0xffff0000);        Spannablestringbuilder Spanbuilder = new Spannablestringbuilder ("This is a Test");        Style 0 is normal, and typeface.bold (bold) Typeface.italic (italic) and so on//size 0 is the  original normal size         Spanbuilder.setspan (new Textappearancespan (null, 0, redcolors, null), 0, 3, spanned.span_exclusive_inclusive);                TextView tv_test = (TextView) Findviewbyid (r.id.tv_test);        Tv_test.settext (Spanbuilder);

The effect is as follows

Android TextView also changes the color and size of some fonts in a string

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.