Textview Set Font Style

Source: Internet
Author: User

 

Textview is used to display text. Sometimes you need to set individual words in textview as hyperlinks, or set the color and font of individual words. Then you need to use the spannable object, you can use the spannable object to implement the above settings.
:

Activity Code:

  1. PackageCom. Zhou. activity;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. Graphics. color;
  4. ImportAndroid. OS. Bundle;
  5. ImportAndroid. Text. spannable;
  6. ImportAndroid. Text. spannablestring;
  7. ImportAndroid. Text. spanned;
  8. ImportAndroid. Text. method. linkmovementmethod;
  9. ImportAndroid. Text. style. backgroundcolorspan;
  10. ImportAndroid. Text. style. foregroundcolorspan;
  11. ImportAndroid. Text. style. stylespan;
  12. ImportAndroid. Text. style. urlspan;
  13. ImportAndroid. widget. textview;
  14. Public ClassTextviewlinkactivityExtendsActivity {
  15. Textview mytextview;
  16. @ Override
  17. Public VoidOncreate (bundle savedinstancestate ){
  18. Super. Oncreate (savedinstancestate );
  19. Setcontentview (R. layout. Main );
  20. Mytextview = (textview)This. Findviewbyid (R. Id. mytextview );
  21. // Create a spannablestring object
  22. Spannablestring sp =NewSpannablestring ("this sentence contains Baidu hyperlink, highlighted, or in italic .");
  23. // Set the hyperlink
  24. Sp. setspan (NewUrlspan ("http://www.baidu.com"), 5, 7,
  25. Spanned. span_exclusive_exclusive );
  26. // Set highlight style 1
  27. Sp. setspan (NewBackgroundcolorspan (color. Red), 17, 19, spannable. span_exclusive_exclusive );
  28. // Set highlight style 2
  29. Sp. setspan (NewForegroundcolorspan (color. Yellow), 20, 24, spannable. span_exclusive_inclusive );
  30. // Set italics
  31. Sp. setspan (NewStylespan (Android. Graphics. typeface. bold_italic), 27, 29, spannable. span_exclusive_inclusive );
  32. // Set the spannablestring object to textview.
  33. Mytextview. settext (SP );
  34. // Set textview to clickable
  35. Mytextview. setmovementmethod (linkmovementmethod. getinstance ());
  36. }
  37. }

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.