1 New Spannablestring ("Test" +xm+ "replacement of the current number will be sent from the mobile phone 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) method to obtain the TextView object byThe SetText () method dynamically assigns values, sets the single-line mode through the Android:singleline property, sets the overall color through the android:textcolor, and sets the automatic connection via the Android:autolink (none, ) of the type.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, there are two main types of ways to understand the format of text:first Category: HTML tag formatting textthe code is relatively simple, as follows:
1 Importandroid.app.Activity; 2 ImportAndroid.os.Bundle; 3 Importandroid.text.Html; 4 ImportAndroid.widget.TextView; 5 6 Public classAndroidfroncolortestextendsActivity {7 @Override8 Public voidonCreate (Bundle savedinstancestate) {9 Super. OnCreate (savedinstancestate); Ten One Setcontentview (R.layout.main); A -TextView Htmlformatetextview =(TextView) Findviewbyid (R.id.testtextview); - theString Source = "This is just a test, test <u> underline </u>, <i> italic word </i>, <font color= ' red ' > Red Word </font> The format "; - - Htmlformatetextview.settext (html.fromhtml (source)); - } +}
The second class is formatted by spannablestring
1 Public classTextviewlinkactivityextendsActivity {2TextView Mtextview =NULL; 3Spannablestring MSP =NULL; 4 5 6 @Override7 Public voidonCreate (Bundle savedinstancestate) {8 Super. OnCreate (savedinstancestate); 9 Setcontentview (R.layout.main); Ten OneMtextview =(TextView) Findviewbyid (R.id.mytextview); A - //Create a Spannablestring object -MSP =NewSpannablestring ("Font test font size half twice times foreground background color normal bold italic bold italic underline strikethrough x1x2 phone mail website sms MMS map x Axis synthesis"); the - //set Font (DEFAULT,DEFAULT-BOLD,MONOSPACE,SERIF,SANS-SERIF) -Msp.setspan (NewTypefacespan ("monospace"), 0, 2, spanned.span_exclusive_exclusive); -Msp.setspan (NewTypefacespan ("serif"), 2, 4, spanned.span_exclusive_exclusive); + - //Set Font size (absolute value, in pixels) +Msp.setspan (NewAbsolutesizespan (20), 4, 6, spanned.span_exclusive_exclusive); AMsp.setspan (NewAbsolutesizespan (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. at - //Set the font size (relative value, units: pixels) parameter to indicate how many times the default font size -Msp.setspan (NewRelativesizespan (0.5f), 8, spanned.span_exclusive_exclusive);//0.5f means half of the default font size -Msp.setspan (NewRelativesizespan (2.0f), ten, spanned.span_exclusive_exclusive);//2.0f indicates twice times the default font size - - //set font foreground color inMsp.setspan (NewForegroundcolorspan (Color.magenta), N, spanned.span_exclusive_exclusive);//set foreground color to magenta - to //set the font background color +Msp.setspan (NewBackgroundcolorspan (Color.cyan), spanned.span_exclusive_exclusive);//set the background color to cyan - the //set font style normal, bold, italic, bold italic *Msp.setspan (NewStylespan (Android.graphics.Typeface.NORMAL), spanned.span_exclusive_exclusive);//Normal $Msp.setspan (NewStylespan (Android.graphics.Typeface.BOLD), spanned.span_exclusive_exclusive);//Bold BodyPanax NotoginsengMsp.setspan (NewStylespan (Android.graphics.Typeface.ITALIC), (spanned.span_exclusive_exclusive);//Italic Body -Msp.setspan (NewStylespan (Android.graphics.Typeface.BOLD_ITALIC), spanned.span_exclusive_exclusive);//Bold Italic Body the + //Set Underline AMsp.setspan (NewUnderlinespan (), 27, 30, spanned.span_exclusive_exclusive); the + //Set Strikethrough -Msp.setspan (NewStrikethroughspan (), 30, 33, spanned.span_exclusive_exclusive); $ $ //set up and down labels -Msp.setspan (NewSubscriptspan (), spanned.span_exclusive_exclusive, (+);// Subscript -Msp.setspan (NewSuperscriptspan (), spanned.span_exclusive_exclusive, Notoginseng;//superscript the - //Hyperlink (need to add Setmovementmethod method additional response)WuyiMsp.setspan (NewUrlspan ("tel:4155551212"), Panax Notoginseng, spanned.span_exclusive_exclusive);//Telephone theMsp.setspan (NewUrlspan ("Mailto:[email protected]"), (a), in all, spanned.span_exclusive_exclusive);//Mail -Msp.setspan (NewUrlspan ("http://www.baidu.com"), (a), spanned.span_exclusive_exclusive);//Network WuMsp.setspan (NewUrlspan ("sms:4155551212"), (a), spanned.span_exclusive_exclusive);//SMS Using SMS: or Smsto: -Msp.setspan (NewUrlspan ("mms:4155551212"), (a), spanned.span_exclusive_exclusive);//MMS using MMS: or Mmsto: AboutMsp.setspan (NewUrlspan ("geo:38.899533,-77.036476"), spanned.span_exclusive_exclusive);//Map $ - //Sets the font size (relative value, units: pixels) The number of times the default font width is represented by the parameter -Msp.setspan (NewScalexspan (2.0f), (spanned.span_exclusive_exclusive);//2.0f means twice times the width of the default font, which is twice times the x-axis, and the height of the default font is unchanged. - //spannablestring Object set to TextView A Mytextview.settext (SP); + //set TextView to click the Mytextview.setmovementmethod (Linkmovementmethod.getinstance ()); - } $}
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).
Note: The source of the article can not be found, please understand