The rich text style of TextView and the specific use of spannablestring

Source: Internet
Author: User

There are several ways to make TextView have a style that is linked in a Web page:

1. Use the layout properties to apply the effect automatically, using the Android:autolink property

< Textview > ... android:autolink= "All" ..... </ Textview >

2. Use <a> tags in string literals

3, in the Java code directly settext

Textview.settext (    html.fromhtml (         "<b>text3:</b>  Text with a" +         "<a href =\ "http://www.google.com\" >link</a> "+         " created in the Java source code using HTML. " ) ); Textview.setmovementmethod (Linkmovementmethod.getinstance ()) ;

4. Achieve rich results by creating spanablestring strings and creating one or more spans above, for example:

New Spannablestring ("Click here to dial the phone.") );   Ss.setspan (new Stylespan (Typeface.bold), 0, 6, spanned.span_exclusive_exclusive);   Ss.setspan (new urlspan ("tel:4155551212"), andspanned.span_exclusive_exclusive);      = (TextView) Findviewbyid (R.ID.TEXT4);   

About the specific use of spannablestring:

Mtextview =(TextView) Findviewbyid (R.id.mytextview); //Create a Spannablestring objectMSP =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/bot"); //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); Msp.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. //Set the font size (relative value, units: pixels) parameter to indicate how many times the default font sizeMsp.setspan (NewRelativesizespan (0.5f), 8, spanned.span_exclusive_exclusive);//0.5f means half of the default font sizeMsp.setspan (NewRelativesizespan (2.0f), ten, spanned.span_exclusive_exclusive);//2.0f indicates twice times the default font size//set font foreground colorMsp.setspan (NewForegroundcolorspan (Color.magenta), N, spanned.span_exclusive_exclusive);//set foreground color to magenta//set the font background colorMsp.setspan (NewBackgroundcolorspan (Color.cyan), spanned.span_exclusive_exclusive);//set the background color to cyan//set font style normal, bold, italic, bold italicMsp.setspan (NewStylespan (Android.graphics.Typeface.NORMAL), spanned.span_exclusive_exclusive);//NormalMsp.setspan (NewStylespan (Android.graphics.Typeface.BOLD), spanned.span_exclusive_exclusive);//Bold BodyMsp.setspan (NewStylespan (Android.graphics.Typeface.ITALIC), (spanned.span_exclusive_exclusive);//Italic BodyMsp.setspan (NewStylespan (Android.graphics.Typeface.BOLD_ITALIC), spanned.span_exclusive_exclusive);//Bold Italic Body//Set UnderlineMsp.setspan (NewUnderlinespan (), 27, 30, spanned.span_exclusive_exclusive); //Set StrikethroughMsp.setspan (NewStrikethroughspan (), 30, 33, spanned.span_exclusive_exclusive); //set up and down labelsMsp.setspan (NewSubscriptspan (), spanned.span_exclusive_exclusive, (+);// SubscriptMsp.setspan (NewSuperscriptspan (), spanned.span_exclusive_exclusive, Notoginseng;//superscript//Hyperlink (need to add Setmovementmethod method additional response)Msp.setspan (NewUrlspan ("tel:4155551212"), Panax Notoginseng, spanned.span_exclusive_exclusive);//TelephoneMsp.setspan (NewUrlspan ("Mailto:[email protected]"), (a), in all, spanned.span_exclusive_exclusive);//MailMsp.setspan (NewUrlspan ("http://www.baidu.com"), (a), spanned.span_exclusive_exclusive);//NetworkMsp.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:Msp.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 parameterMsp.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.//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 ExceptionE.printstacktrace (); }Catch(IOException e) {//Todo:handle ExceptionE.printstacktrace (); } xmlresourceparser Xpplinkcolor=getresources (). GETXML (R.color.linkcolor); Try{Csllink=Colorstatelist.createfromxml (Getresources (), xpplinkcolor); }Catch(xmlpullparserexception e) {//Todo:handle ExceptionE.printstacktrace (); }Catch(IOException e) {//Todo:handle ExceptionE.printstacktrace (); } msp.setspan (NewTextappearancespan ("Monospace", Android.graphics.Typeface.BOLD_ITALIC,, CSL, Csllink), 51, 53, spanned.span_exclusive_exclusive); //Set BulletsMsp.setspan (NewBulletspan (Android.text.style.bulletspan.standard_gap_width,color.green), 0, Msp.length (), Spanned.SPAN_EXCLUSIVE _exclusive);
//The first parameter represents the width of the bullet, and the second parameter is the color of the bullet//set up a pictureDrawable drawable =getresources (). getdrawable (R.drawable.icon); Drawable.setbounds (0, 0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); Msp.setspan (NewImagespan (drawable), 53, 57, spanned.span_exclusive_exclusive); Mtextview.settext (MSP); Mtextview.setmovementmethod (Linkmovementmethod.getinstance ());

The rich text style of TextView and the specific use of spannablestring

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.