Android TextView set different font sizes and colors

Source: Internet
Author: User

In real-world applications, you need to display a string with a different color and font. Of course, it can be stitched together by different textview. Can also be shown through a textview.

The steps are as follows:

1. Define different style.

You might want to define 2 style as follows

        <style name= "Style0" >    <item name= "android:textsize" >19dip</item>    <item name= "Android : TextColor "> @color/color1</item>        </style><style name=" Style1 ">    <item name=" Android:textsize ">23dip</item>    <item name=" Android:textcolor "> @color/color2</item>    <item name= "Android:textstyle" >italic</item></style>

  

2. Format the string with spannablestring. The code is as follows:

Mtextview = (TextView) Findviewbyid (r.id.test); spannablestring styledtext = new Spannablestring ("Android, hello"); Styledtext.setspan (New Textappearancespan (this, R.STYLE.STYLE0), 0, 3, spanned.span_exclusive_exclusive); Styledtext.setspan (New Textappearancespan (this, r.style.style1), 3, 5, spanned.span_exclusive_exclusive); Mtextview.settext (Styledtext, TextView.BufferType.SPANNABLE);

  

Reference: http://blog.csdn.net/fengyoujie/article/details/42783891

Android TextView set different font sizes and colors

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.