效果图如下:

Android TextView 实现文字大小不同和文字颜色不同_html

关键代码如下:

 




[java] ​​view plain​​​​copy​​​​​​​​​

 


  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.   
  9.                 sb.append("<a href=\"2\">");  
  10.                 if(hour > 9) {  
  11.                     sb.append(" "+hour);  
  12.                 } else {  
  13.                     sb.append(" 0"+hour);  
  14.                 }  
  15.                 sb.append("</a>");  
  16.                 sb.append(""+Global.getString(R.string.unit_Hour2));  
  17.   
  18.                 sb.append("<a href=\"3\">");  
  19.                 if(minute > 9) {  
  20.                     sb.append(" "+minute);  
  21.                 } else {  
  22.                     sb.append(" 0"+minute);  
  23.                 }  
  24.                 sb.append("</a>");  
  25.                 sb.append(""+Global.getString(R.string.unit_Minute2));  
  26.   
  27.                 sb.append("<a href=\"4\">");  
  28.                 if(second > 9) {  
  29.                     sb.append(" " + second);  
  30.                 } else {  
  31.                     sb.append(" 0" + second);  
  32.                 }  
  33.                 sb.append("</a>");  
  34.                 sb.append(""+Global.getString(R.string.unit_Second));  
  35.   
  36.                 int end = sb.length();  
  37.                 SpannableString ss = new SpannableString(Html.fromHtml(sb.toString()));  
  38.                 URLSpan[] urls = ss.getSpans(0, end, URLSpan.class);  
  39.   
  40.                 String resultStr = sb.toString();  
  41.                 Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);  
  42.                 Matcher m_html = p_html.matcher(resultStr);  
  43.                 resultStr = m_html.replaceAll(""); // 过滤html标签  
  44.                 SpannableString span = new SpannableString(resultStr);  
  45.                 for (URLSpan url : urls) {  
  46.                     int startIndex = ss.getSpanStart(url);  
  47.                     int endIndex = ss.getSpanEnd(url);  
  48.                     // 原字两倍大小  
  49.                     span.setSpan(new AbsoluteSizeSpan(Global.dpToPx(24)), startIndex, endIndex,  
  50.                             Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  51.                     // 设置样式2  
  52.                     span.setSpan(new ForegroundColorSpan(Color.WHITE),  
  53.                             startIndex, endIndex,  
  54.                             Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  
  55.                     // 设置样式3  
  56.                     span.setSpan(new StyleSpan(Typeface.BOLD),  
  57.                             startIndex, endIndex,  
  58.                             Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  
  59.                 }  
  60.   
  61.                 mBottomLL2_Detail.setText(span);  


说明:

 

1.主要原理为使用SpannableString来做多样式的String给TextView显示

2.用到<a href>标签的原因是为了做到国际化,因为span是按照index来多样化的,所以需要用href来标识多样化的index