Android TextView font color and other styles

Source: Internet
Author: User

Android TextView font color and other styles

For Android, you can set multiple color fonts for each TextView text.

Code used by SpannableString in TextView

 

// Create a SpannableString object SpannableString msp = new SpannableString (font test font size half twice foreground color background color normal bold italic underline strikethrough x1x2 telephone mail website sms mms Map x axis synthesis ); // set the font (default, default-bold, monospace, serif, sans-serif) msp. setSpan (new TypefaceSpan (monospace), 0, 2, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); msp. setSpan (new TypefaceSpan (serif), 2, 4, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // sets the font size (absolute value, unit: pixels). msp. setSpan (new AbsoluteSizeSpan (20), 4, 6, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); msp. setSpan (new AbsoluteSizeSpan (20, true), 6, 8, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // The second parameter boolean dip. If it is true, it indicates that the front font size is in dip; otherwise, it is pixel, same as above. // Set the font size (relative value, unit: pixel). The parameter indicates how many times the default font size msp. setSpan (new RelativeSizeSpan (0.5f), 8, 10, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 0.5f indicates half of the default font size. setSpan (new RelativeSizeSpan (2.0f), 10, 12, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f indicates twice the default font size. // set the foreground color of the font msp. setSpan (new ForegroundColorSpan (Color. MAGENTA), 12, 15, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // set the foreground color to magenta // set the font background color msp. setSpan (new BackgroundColorSpan (Color. CYAN), 15, 18, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // set the background color to cyan // set the font style to normal, bold, italic, and bold italic. setSpan (new StyleSpan (android. graphics. typeface. NORMAL), 18, 20, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // normal msp. setSpan (new StyleSpan (android. graphics. typeface. BOLD), 20, 22, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // specifies the name in bold. setSpan (new StyleSpan (android. graphics. typeface. ITALIC), 22, 24, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // specifies the family name in italic. setSpan (new StyleSpan (android. graphics. typeface. BOLD_ITALIC), 24, 27, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // bold italic // set the underline msp. setSpan (new UnderlineSpan (), 27, 30, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // you can specify the MSPs. setSpan (new StrikethroughSpan (), 30, 33, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // sets the upper and lower mark msp. setSpan (new SubscriptSpan (), 34, 35, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // subscript msp. setSpan (new SuperscriptSpan (), 36, 37, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // superscript // hyperlink (you need to add the setMovementMethod Method to the response) msp. setSpan (new URLSpan (tel: 4155551212), 37, 39, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // call msp. setSpan (new URLSpan (mailto: webmaster@google.com), 39, 41, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mail msp. setSpan (new URLSpan (http://www.baidu.com), 41, 43, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // network msp. setSpan (new URLSpan (sms: 4155551212), 43, 45, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // sms: Or smsto: msp. setSpan (new URLSpan (mms: 4155551212), 45, 47, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mms: Or mmsto: msp. setSpan (new URLSpan (geo: 38.899533,-77.036476), 47, 49, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // map // set the font size (relative value, unit: pixels). The parameter indicates how many times the default font width is msp. setSpan (new ScaleXSpan (2.0f), 49, 51, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f indicates twice the default font width, that is, double the default font width in the X axis direction, and the height remains unchanged. // set the project symbol msp. setSpan (new BulletSpan (android. text. style. bulletSpan. STANDARD_GAP_WIDTH, Color. GREEN), 0, 53, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // The first parameter indicates the width occupied by the Project symbol, and the second parameter is the color TextView view1 = (TextView) findViewById (R. id. tv1); view1.setText (msp); view1.setMovementMethod (LinkMovementMethod. getInstance ());

 

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.