Android TextView Achieve different text sizes and text colors

Source: Internet
Author: User

As follows:

The key code is as follows:

[Java] view Plaincopy 
  1. StringBuffer sb = new StringBuffer ();
  2. if (Day > 0) {
  3. Sb.append ("<a href=\" 1\ ">");
  4. Sb.append ("+day");
  5. Sb.append ("</a>");
  6. Sb.append ("+global.getstring (r.string.unit_day2)");
  7. }
  8. Sb.append ("<a href=\" 2\ ">");
  9. if (Hour > 9) {
  10. Sb.append ("+hour");
  11. } Else {
  12. Sb.append ("0" +hour);
  13. }
  14. Sb.append ("</a>");
  15. Sb.append ("+global.getstring (R.STRING.UNIT_HOUR2)");
  16. Sb.append ("<a href=\" 3\ ">");
  17. if (Minute > 9) {
  18. Sb.append ("+minute");
  19. } Else {
  20. Sb.append ("0" +minute);
  21. }
  22. Sb.append ("</a>");
  23. Sb.append ("+global.getstring (R.string.unit_minute2)");
  24. Sb.append ("<a href=\" 4\ ">");
  25. if (Second > 9) {
  26. Sb.append ("" + second);
  27. } Else {
  28. Sb.append ("0" + second);
  29. }
  30. Sb.append ("</a>");
  31. Sb.append ("+global.getstring (R.string.unit_second)");
  32. int end = Sb.length ();
  33. spannablestring ss = New Spannablestring (html.fromhtml (sb.tostring ()));
  34. urlspan[] urls = Ss.getspans (0, End, Urlspan.   Class);
  35. String resultstr = sb.tostring ();
  36. Pattern p_html = Pattern.compile (regex_html, pattern.case_insensitive);
  37. Matcher m_html = P_html.matcher (RESULTSTR);
  38. ResultStr = M_html.replaceall (""); //Filter HTML tags
  39. Spannablestring span = new spannablestring (RESULTSTR);
  40. For (Urlspan url:urls) {
  41. int startIndex = Ss.getspanstart (URL);
  42. int endIndex = ss.getspanend (URL);
  43. //twice times the original word size
  44. Span.setspan (new Absolutesizespan (global.dptopx), StartIndex, EndIndex,
  45. spannable.span_exclusive_exclusive);
  46. //Set style 2
  47. Span.setspan (new Foregroundcolorspan (color.white),
  48. StartIndex, EndIndex,
  49. spannable.span_exclusive_inclusive);
  50. //Set style 3
  51. Span.setspan (new Stylespan (typeface.bold),
  52. StartIndex, EndIndex,
  53. spannable.span_exclusive_inclusive);
  54. }
  55. Mbottomll2_detail.settext (span);


Description

1. The main principle is to use spannablestring to long style string to TextView display

2. The reason for using the <a href> label is to be internationalized, because span is diversified according to index, so it is necessary to use HREF to identify a variety of index

Android TextView Achieve different text sizes and text colors

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.