In Android, TextView is the most common control we use to display text.
In general, the text in TextView is a style. So how do you set the font, size, color, style, and hyperlink properties for each part of the TextView text? Let's demonstrate this by spannablestring specific examples.
Package com.snowdream;
Import java.io.IOException;
Import org.xmlpull.v1.XmlPullParserException;
Import android.app.Activity;
Import android.content.res.ColorStateList;
Import Android.content.res.XmlResourceParser;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import android.text.SpannableString;
Import android.text.Spanned;
Import Android.text.method.LinkMovementMethod;
Import Android.text.style.AbsoluteSizeSpan;
Import Android.text.style.BackgroundColorSpan;
Import Android.text.style.BulletSpan;
Import Android.text.style.ForegroundColorSpan;
Import Android.text.style.RelativeSizeSpan;
Import Android.text.style.ScaleXSpan;
Import Android.text.style.StrikethroughSpan;
Import Android.text.style.StyleSpan;
Import Android.text.style.SubscriptSpan;
Import Android.text.style.SuperscriptSpan;
Import Android.text.style.TextAppearanceSpan;
Import Android.text.style.TypefaceSpan;
Import Android.text.style.URLSpan;
Import Android.text.style.UnderlineSpan;
Import Android.widget.TextView;
public class Textviewlinkactivity extends Activity {
TextView Mtextview = null;
Spannablestring MSP = null;
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mtextview = (TextView) Findviewbyid (R.id.mytextview);
Create a Spannablestring object
MSP = new spannablestring (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 ");
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);
Set font size (absolute value, in pixels)
Msp.setspan (New Absolutesizespan (), 4, 6, spanned.span_exclusive_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.
Set the font size (relative value, units: pixels) parameter to indicate how many times the default font size
Msp.setspan (New Relativesizespan (0.5f), 8, ten, spanned.span_exclusive_exclusive); 0.5f means half of the default font size
Msp.setspan (New Relativesizespan (2.0f), ten, spanned.span_exclusive_exclusive); 2.0F indicates twice times the default font size
Set font foreground color
Msp.setspan (New Foregroundcolorspan (Color.magenta), spanned.span_exclusive_exclusive); Set foreground color to magenta
Set the font background color
Msp.setspan (New Backgroundcolorspan (Color.cyan), spanned.span_exclusive_exclusive); Set the background color to cyan
Set font style Normal, bold, italic, bold italic
Msp.setspan (New Stylespan (Android.graphics.Typeface.NORMAL), +, spanned.span_exclusive_exclusive); Normal
Msp.setspan (New Stylespan (Android.graphics.Typeface.BOLD), spanned.span_exclusive_exclusive); Bold body
Msp.setspan (New Stylespan (Android.graphics.Typeface.ITALIC), N, spanned.span_exclusive_exclusive); Italic body
Msp.setspan (New Stylespan (Android.graphics.Typeface.BOLD_ITALIC), +, spanned.span_exclusive_exclusive); Bold Italic Body
Set underline
Msp.setspan (New Underlinespan (), +, spanned.span_exclusive_exclusive);
Set Strikethrough
Msp.setspan (New Strikethroughspan (), (a), spanned.span_exclusive_exclusive);
Set up and down labels
Msp.setspan (New Subscriptspan (), spanned.span_exclusive_exclusive); Subscript
Msp.setspan (New Superscriptspan (), $, Notoginseng, spanned.span_exclusive_exclusive); Superscript
Hyperlink (need to add Setmovementmethod method additional response)
Msp.setspan (New Urlspan ("tel:4155551212"), Panax Notoginseng, spanned.span_exclusive_exclusive); Phone
Msp.setspan (New Urlspan ("Mailto:[email protected]"), (a); Mail
Msp.setspan (New Urlspan ("http://www.baidu.com"), (a), spanned.span_exclusive_exclusive); Internet
Msp.setspan ("sms:4155551212"), Urlspan, spanned.span_exclusive_exclusive); SMS Using SMS: or Smsto:
Msp.setspan (New Urlspan ("mms:4155551212"), A, A, spanned.span_exclusive_exclusive); MMS using MMS: or Mmsto:
Msp.setspan (New Urlspan ("geo:38.899533,-77.036476"), N, a, 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 (New Scalexspan (2.0f), Yi, Yi, 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.
Set the font (including font name, font size, font style, font color, link color)
Colorstatelist csllink = null;
Colorstatelist CSL = null;
Xmlresourceparser xppcolor=getresources (). GETXML (R.color.color);
try {
Csl= Colorstatelist.createfromxml (Getresources (), xppcolor);
}catch (Xmlpullparserexception e) {
Todo:handle exception
E.printstacktrace ();
}catch (IOException e) {
Todo:handle exception
E.printstacktrace ();
}
Xmlresourceparser xpplinkcolor=getresources (). GETXML (R.color.linkcolor);
try {
csllink= Colorstatelist.createfromxml (Getresources (), xpplinkcolor);
}catch (Xmlpullparserexception e) {
Todo:handle exception
E.printstacktrace ();
}catch (IOException e) {
Todo:handle exception
E.printstacktrace ();
}
Msp.setspan ("Monospace", Android.graphics.Typeface.BOLD_ITALIC, Textappearancespan, CSL, Csllink), 51, 53, spanned.span_exclusive_exclusive);
Set Bullets
Msp.setspan (New Bulletspan (Android.text.style.bulletspan.standard_gap_width,color.green), 0, Spanned.SPAN_ exclusive_exclusive); The first parameter represents the width of the bullet, and the second parameter is the color of the bullet
Mtextview.settext (MSP);
Mtextview.setmovementmethod (Linkmovementmethod.getinstance ());
}
}
Text in Android TextView by spannablestring to set hyperlinks, colors, fonts, etc.