Android TextView 实现文字大小不同和文字颜色不同
2014-06-21 14:56
555 查看
效果图如下:
关键代码如下:
说明:
1.主要原理为使用SpannableString来做多样式的String给TextView显示
2.用到<a href>标签的原因是为了做到国际化,因为span是按照index来多样化的,所以需要用href来标识多样化的index
关键代码如下:
StringBuffer sb = new StringBuffer(); if(day > 0) { sb.append("<a href=\"1\">"); sb.append(""+day); sb.append("</a>"); sb.append(""+Global.getString(R.string.unit_Day2)); } sb.append("<a href=\"2\">"); if(hour > 9) { sb.append(" "+hour); } else { sb.append(" 0"+hour); } sb.append("</a>"); sb.append(""+Global.getString(R.string.unit_Hour2)); sb.append("<a href=\"3\">"); if(minute > 9) { sb.append(" "+minute); } else { sb.append(" 0"+minute); } sb.append("</a>"); sb.append(""+Global.getString(R.string.unit_Minute2)); sb.append("<a href=\"4\">"); if(second > 9) { sb.append(" " + second); } else { sb.append(" 0" + second); } sb.append("</a>"); sb.append(""+Global.getString(R.string.unit_Second)); int end = sb.length(); SpannableString ss = new SpannableString(Html.fromHtml(sb.toString())); URLSpan[] urls = ss.getSpans(0, end, URLSpan.class); String resultStr = sb.toString(); Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE); Matcher m_html = p_html.matcher(resultStr); resultStr = m_html.replaceAll(""); // 过滤html标签 SpannableString span = new SpannableString(resultStr); for (URLSpan url : urls) { int startIndex = ss.getSpanStart(url); int endIndex = ss.getSpanEnd(url); // 原字两倍大小 span.setSpan(new AbsoluteSizeSpan(Global.dpToPx(24)), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置样式2 span.setSpan(new ForegroundColorSpan(Color.WHITE), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); // 设置样式3 span.setSpan(new StyleSpan(Typeface.BOLD), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); } mBottomLL2_Detail.setText(span);
说明:
1.主要原理为使用SpannableString来做多样式的String给TextView显示
2.用到<a href>标签的原因是为了做到国际化,因为span是按照index来多样化的,所以需要用href来标识多样化的index
相关文章推荐
- Android TextView 实现文字大小不同和文字颜色不同
- Android TextView 实现文字大小不同和文字颜色不同
- Android TextView 实现文字大小不同和文字颜色不同
- Android中TextView中部分文字有不同颜色的实现
- android 在一个textview中实现不同的颜色和大小
- Android中TextView中部分文字有不同颜色的实现
- android中一个TextView实现其中内容大小颜色不同的效果
- Android中TextView中部分文字有不同颜色的实现
- Textview增加图片并实现点击与文字设置不同大小颜色
- Android中TextView中部分文字有不同颜色的实现
- Android 一个TextView中设置文字不同字体大小和颜色的最完整方法
- Android中TextView中部分文字有不同颜色的实现
- Android textview设置不同的颜色和大小
- android 利用一个TextView实现两行并且每行的字体大小和颜色各异
- Android Textview实现文字颜色渐变效果
- Android一个TextView文字颜色不同或多个关键字不同颜色
- Android TextView 字符串展示不同大小文字
- android中textview的文字处理--同一段文字部分文字作高亮或者显示不同大小等
- android 一个 textview 设置不同的字体大小和颜色
- 关于一个Textview内的文字设置不同大小和颜色