[Java]
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. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Color;
Import android. graphics. drawable. Drawable;
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. DrawableMarginSpan;
Import android. text. style. ForegroundColorSpan;
Import android. text. style. IconMarginSpan;
Import android. text. style. ImageSpan;
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 foreground color background color normal bold italic underline strikethrough line x1x2 telephone mail website sms mms Map x axis integrated/bot ");
// 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 );
// Set the font size (absolute value, unit: pixel)
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) to the number of 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
Msp. setSpan (new RelativeSizeSpan (2.0f), 10, 12, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f indicates twice the default font size
// Set the font foreground color
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 blue
// Set the font style to normal, bold, italic, and bold.
Msp. 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); // BOLD
Msp. setSpan (new StyleSpan (android. graphics. Typeface. ITALIC), 22, 24, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // ITALIC
Msp. 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 );
// Set strikethrough
Msp. setSpan (new StrikethroughSpan (), 30, 33, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Set the upper/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 (the setMovementMethod method must be added for 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.bkjia.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: pixel) to the number of times the default font width.
Msp. setSpan (new ScaleXSpan (2.0f), 49, 51, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f indicates that the default font width is twice, that is, the X axis is enlarged to twice the default font width, and the height remains unchanged.
// Set the font (including the font name, font size, Font Style, font color, and link color in sequence)
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 (new TextAppearanceSpan ("monospace", android. graphics. Typeface. BOLD_ITALIC, 30, csl, csllink), 51, 53, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Set the project symbol
Msp. setSpan (new BulletSpan (android. text. style. bulletSpan. STANDARD_GAP_WIDTH, Color. GREEN), 0, msp. length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // The first parameter indicates the width occupied by the Project symbol, and the second parameter indicates the color of the Project symbol.
// Set the image
Drawable drawable = getResources (). getDrawable (R. drawable. icon );
Drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight ());
Msp. setSpan (new ImageSpan (drawable), 53, 57, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
MTextView. setText (msp );
MTextView. setMovementMethod (LinkMovementMethod. getInstance ());
}
}
Effect preview:
From tody_guo's column