Android TextView text through spannablestring to set hyperlinks, colors, fonts, etc.

Source: Internet
Author: User

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.

Res-layout-main.xml

    1. <?xml version= "1.0" encoding= "Utf-8"?>

    2. <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

    3. android:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:orientation= "Horizontal" >

    4. <textview

    5. Android:id= "@+id/mytextview"

    6. Android:layout_width= "Fill_parent"

    7. android:layout_height= "Wrap_content"

    8. />

    9. </LinearLayout>

Res-color-color.xml

Res-color-linkcolor.xml

View Plainprint?

    1. <?xml version= "1.0" encoding= "Utf-8"?>

    2. <selector xmlns:android= "Http://schemas.android.com/apk/res/android" >

    3. <item android:state_pressed= "true"

    4. Android:color= "#ffffff00"/> <!--pressed--

    5. <item android:state_focused= "true"

    6. Android:color= "#ff00ffff"/> <!--focused--

    7. <item android:color= "#ff0ff000"/> <!--Default---

    8. </selector>

Textviewlinkactivity

View Plainprint?

  1. Package com.snowdream;


  2. Import java.io.IOException;


  3. Import org.xmlpull.v1.XmlPullParserException;


  4. Import android.app.Activity;

  5. Import android.content.res.ColorStateList;

  6. Import Android.content.res.XmlResourceParser;

  7. Import Android.graphics.Color;

  8. Import Android.os.Bundle;

  9. Import android.text.SpannableString;

  10. Import android.text.Spanned;

  11. Import Android.text.method.LinkMovementMethod;

  12. Import Android.text.style.AbsoluteSizeSpan;

  13. Import Android.text.style.BackgroundColorSpan;

  14. Import Android.text.style.BulletSpan;

  15. Import Android.text.style.ForegroundColorSpan;

  16. Import Android.text.style.RelativeSizeSpan;

  17. Import Android.text.style.ScaleXSpan;

  18. Import Android.text.style.StrikethroughSpan;

  19. Import Android.text.style.StyleSpan;

  20. Import Android.text.style.SubscriptSpan;

  21. Import Android.text.style.SuperscriptSpan;

  22. Import Android.text.style.TextAppearanceSpan;

  23. Import Android.text.style.TypefaceSpan;

  24. Import Android.text.style.URLSpan;

  25. Import Android.text.style.UnderlineSpan;

  26. Import Android.widget.TextView;


  27. public class Textviewlinkactivity extends Activity {

  28. TextView Mtextview = null;

  29. Spannablestring MSP = null;


  30. /** called when the activity is first created. */

  31. @Override

  32. public void OnCreate (Bundle savedinstancestate) {

  33. Super.oncreate (savedinstancestate);

  34. Setcontentview (R.layout.main);


  35. Mtextview = (TextView) Findviewbyid (R.id.mytextview);


  36. Create a Spannablestring object

  37. 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 ");


  38. Set Font (DEFAULT,DEFAULT-BOLD,MONOSPACE,SERIF,SANS-SERIF)

  39. Msp.setspan (New Typefacespan ("Monospace"), 0, 2, spanned.span_exclusive_exclusive);

  40. Msp.setspan (New Typefacespan ("serif"), 2, 4, spanned.span_exclusive_exclusive);


  41. Set font size (absolute value, in pixels)

  42. Msp.setspan (New Absolutesizespan (), 4, 6, spanned.span_exclusive_exclusive);

  43. 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.


  44. Set the font size (relative value, units: pixels) parameter to indicate how many times the default font size

  45. Msp.setspan (New Relativesizespan (0.5f), 8, ten, spanned.span_exclusive_exclusive); 0.5f means half of the default font size

  46. Msp.setspan (New Relativesizespan (2.0f), ten, spanned.span_exclusive_exclusive); 2.0F indicates twice times the default font size


  47. Set font foreground color

  48. Msp.setspan (New Foregroundcolorspan (Color.magenta), spanned.span_exclusive_exclusive); Set foreground color to magenta


  49. Set the font background color

  50. Msp.setspan (New Backgroundcolorspan (Color.cyan), spanned.span_exclusive_exclusive); Set the background color to cyan


  51. Set font style Normal, bold, italic, bold italic

  52. Msp.setspan (New Stylespan (Android.graphics.Typeface.NORMAL), +, spanned.span_exclusive_exclusive); Normal

  53. Msp.setspan (New Stylespan (Android.graphics.Typeface.BOLD), spanned.span_exclusive_exclusive); Bold body

  54. Msp.setspan (New Stylespan (Android.graphics.Typeface.ITALIC), N, spanned.span_exclusive_exclusive); Italic body

  55. Msp.setspan (New Stylespan (Android.graphics.Typeface.BOLD_ITALIC), +, spanned.span_exclusive_exclusive); Bold Italic Body


  56. Set underline

  57. Msp.setspan (New Underlinespan (), +, spanned.span_exclusive_exclusive);


  58. Set Strikethrough

  59. Msp.setspan (New Strikethroughspan (), (a), spanned.span_exclusive_exclusive);


  60. Set up and down labels

  61. Msp.setspan (New Subscriptspan (), spanned.span_exclusive_exclusive); Subscript

  62. Msp.setspan (New Superscriptspan (), $, Notoginseng, spanned.span_exclusive_exclusive); Superscript


  63. Hyperlink (need to add Setmovementmethod method additional response)

  64. Msp.setspan (New Urlspan ("tel:4155551212"), Panax Notoginseng, spanned.span_exclusive_exclusive); Phone

  65. Msp.setspan (New Urlspan ("Mailto:[email protected]"), (a); Mail

  66. Msp.setspan (New Urlspan ("http://www.baidu.com"), (a), spanned.span_exclusive_exclusive); Internet

  67. Msp.setspan ("sms:4155551212"), Urlspan, spanned.span_exclusive_exclusive); SMS Using SMS: or Smsto:

  68. Msp.setspan (New Urlspan ("mms:4155551212"), A, A, spanned.span_exclusive_exclusive); MMS using MMS: or Mmsto:

  69. Msp.setspan (New Urlspan ("geo:38.899533,-77.036476"), N, a, spanned.span_exclusive_exclusive); Map


  70. Sets the font size (relative value, units: pixels) The number of times the default font width is represented by the parameter

  71. 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.


  72. Set the font (including font name, font size, font style, font color, link color)

  73. Colorstatelist csllink = null;

  74. Colorstatelist CSL = null;

  75. Xmlresourceparser xppcolor=getresources (). GETXML (R.color.color);

  76. try {

  77. Csl= Colorstatelist.createfromxml (Getresources (), xppcolor);

  78. }catch (Xmlpullparserexception e) {

  79. Todo:handle exception

  80. E.printstacktrace ();

  81. }catch (IOException e) {

  82. Todo:handle exception

  83. E.printstacktrace ();

  84. }


  85. Xmlresourceparser xpplinkcolor=getresources (). GETXML (R.color.linkcolor);

  86. try {

  87. csllink= Colorstatelist.createfromxml (Getresources (), xpplinkcolor);

  88. }catch (Xmlpullparserexception e) {

  89. Todo:handle exception

  90. E.printstacktrace ();

  91. }catch (IOException e) {

  92. Todo:handle exception

  93. E.printstacktrace ();

  94. }

  95. Msp.setspan ("Monospace", Android.graphics.Typeface.BOLD_ITALIC, Textappearancespan, CSL, Csllink), 51, 53, spanned.span_exclusive_exclusive);


  96. Set Bullets

  97. 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



  98. Mtextview.settext (MSP);

  99. Mtextview.setmovementmethod (Linkmovementmethod.getinstance ());

  100. }

  101. }


Text in Android TextView by spannablestring to set hyperlinks, colors, fonts, etc.

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.