In Android, textview is the most common control used to display text.
In general, the text in textview is a style. How can we set the font, size, color, style, hyperlink, and other attributes for each part of textview text? The following is an example of spannablestring.
Res-layout-main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal"><TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
Res-color-color.xml
Res-color-linkcolor.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#ffffff00"/> <!-- pressed --> <item android:state_focused="true" android:color="#ff00ffff"/> <!-- focused --> <item android:color="#ff0ff000"/> <!-- default --></selector>
Textviewlinkactivity
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. bitmap; import android. graphics. bitmapfactory; import android. graphics. color; import android. graphics. drawable. drawable; import android. OS. bundle; import android. text. spanna Blestring; 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. st YLE. 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 ("Word Body test font size half twice foreground color background color normal bold italic underline strikethrough x1x2 telephone mail website sms mms Map x axis synthesis/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); // sets the font size (absolute value, unit: pixels). MSP. setspan (New absolutesizespan (20), 4, 6, spanned. span_exclusive_exclusive); MSP. setspan (New absol Utesizespan (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, to enlarge the X axis to twice the default font width, while the height remains unchanged. // set the font (including the font name in turn, 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 (New textappearancespan ("monospace", android. graphics. typeface. bold_italic, 30, CSL, csllink), 51, 53, spanned. span_exclusive_exclusive); // sets 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 is the color of the Project symbol. // set the image 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: