"Android" TextView set individual font styles

Source: Internet
Author: User
Tags set background

1 spannablestring MSP = new spannablestring ("Test" +xm+ "replacement of the current number will be sent from the phone with a regular text message to verify"); 2 Msp.setspan (New Foregroundcolorspan ( Color.Blue), 2, Xm.length () +2, spanned.span_exclusive_exclusive);

The textview:textview of the impression is the control used to display the text, which can be set in the layout file via the Android:text property, or through the context object's Findviewbyid (XXX) After the TextView object is obtained, the SetText () method is used to dynamically assign values, set the single-line mode through the Android:singleline property, and set the overall color through the Android:textcolor via Android: Autolink to set the type of automatic connection (none). Never thought about displaying text in a variety of ways in a TextView control. Textview further deepening: Textview can format its text. By querying the data, the way to understand the format of the text is divided into two main categories: the first Category: HTML tags Formatted text code is relatively simple, as follows:

  

1 import android.app.Activity;    2 import Android.os.Bundle;    3 Import android.text.Html;    4 Import Android.widget.TextView;    5    6 public class Androidfroncolortest extends Activity {    7     @Override   8 public     void OnCreate (Bundle Savedinstancestate) {    9         super.oncreate (savedinstancestate);   Ten         Setcontentview (r.layout.main);         TextView Htmlformatetextview = (TextView) Findviewbyid (R.id.testtextview);         String Source = "This is just a test, test <u> underline </u>, <i> Italic </i>, <font color= ' red ' > Red word </font> format ";         Htmlformatetextview.settext (html.fromhtml (source));   19}  

The second class is formatted by spannablestring

  

 1 public class Textviewlinkactivity extends Activity {2 TextView mtextview = null;     3 spannablestring MSP = null; 4 5 6 @Override 7 public void OnCreate (Bundle savedinstancestate) {8 Super.oncreate (sav   Edinstancestate);  9 Setcontentview (R.layout.main);  Ten Mtextview = (TextView) Findviewbyid (R.id.mytextview); 12 13//Create a Spannablestring object. msp = new spannablestring ("Font test font size half twice times foreground background color normal bold italic bold italic italic underline Delete   Line x1x2 phone mail website sms MMS map X-axis synthesis "); 15 16//Set 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); 19 20//Set font size (absolute value, unit: pixels) Msp.setspan (new Absolutesizespan), 4, 6, Spanned.span_exclusiv  e_exclusive); Msp.setspan (New Absolutesizespan (20,trUE), 6, 8, spanned.span_exclusive_exclusive);  The second parameter, Boolean dip, if true, indicates that the preceding font size is in dip, otherwise pixels, ditto. 23 24//Set font size (relative value, units: pixels) parameter represents how many times the default font size is Msp.setspan (new Relativesizespan (0.5f), 8, Spa nned.  span_exclusive_exclusive);  0.5f represents half of the default font size Msp.setspan (new Relativesizespan (2.0f), ten, spanned.span_exclusive_exclusive); 2.0f means twice times the default font size 27 28//Set font foreground color Msp.setspan (new Foregroundcolorspan (Color.magenta), 12, 1  5, spanned.span_exclusive_exclusive); Set foreground color to magenta 30 31//Set font background color Msp.setspan (new Backgroundcolorspan (Color.cyan), Spanne  d.span_exclusive_exclusive); Set background color to cyan 33 34//Set font style normal, bold, italic, Bold italic Msp.setspan (new Stylespan (Android.graphics.Typeface.NORM  AL), spanned.span_exclusive_exclusive);  Normal Msp.setspan (new Stylespan (Android.graphics.Typeface.BOLD), spanned.span_exclusive_exclusive); Gross Panax Notoginseng msp.setspAn (new Stylespan (Android.graphics.Typeface.ITALIC), N, spanned.span_exclusive_exclusive); Italic Msp.setspan (New Stylespan (Android.graphics.Typeface.BOLD_ITALIC), Spanned.span_exclusive_exclusiv  E); Bold Italic 39 40//Set underline Msp.setspan (new Underlinespan (), +, Spanned.span_exclusive_exclusi  VE); 42 43//Set Strikethrough Msp.setspan (new Strikethroughspan (), +, spanned.span_exclusive_exclusive)  ;     45 46//Set up and down superscript msp.setspan (new Subscriptspan (), spanned.span_exclusive_exclusive);   Subscript-Msp.setspan (New Superscriptspan (), spanned.span_exclusive_exclusive, Panax Notoginseng); Superscript 49 50//hyperlink (need to add Setmovementmethod method additional response) Wuyi Msp.setspan (New Urlspan ("tel:415     5551212 "), Panax Notoginseng, spanned.span_exclusive_exclusive); Phone Msp.setspan (New Urlspan ("mailto:[email protected]"), (a), A-p, spanned.span_exclusive_exclusive);     E-mail Msp.setspan (new Urlspan ("http://www.baidu.com"), all in, spanned.span_exclusive_exclusive);     Network Msp.setspan (New Urlspan ("sms:4155551212"), (a), spanned.span_exclusive_exclusive);     SMS Using SMS: or smsto:55 Msp.setspan (new Urlspan ("mms:4155551212"), (a), +, spanned.span_exclusive_exclusive); MMS using mms: or mmsto:56 Msp.setspan (new Urlspan ("geo:38.899533,-77.036476"), +, spanned.span_exclusive_e     Xclusive);  Map 57 58//Set Font size (relative value, units: pixels) The parameter represents how many times the default font width is Msp.setspan (new Scalexspan (2.0f), 49, 51, spanned.span_exclusive_exclusive);   2.0f indicates the default font width of twice times, that is, the x-axis direction is twice times the default font, and the height of the//spannablestring object set to TextView mytextview.settext (SP);   62//Set TextView can click Mytextview.setmovementmethod (linkmovementmethod.getinstance ()); 64}65}

  

When using Spannablestring objects, be aware that
The role of spanned.span_exclusive_exclusive, etc.:
Used to identify whether or not to use this effect when entering new characters before and after text in a span range. Spanned.span_exclusive_exclusive (not included), Spanned.span_inclusive_exclusive (previously included, not included), Spanned.span_ Exclusive_inclusive (not included in front, included later), Spanned.span_inclusive_inclusive (included before and after).

"Android" TextView set individual font styles

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.