在应用开发中时常会遇到需要在一段文字中插入几个不一样颜色文字的需求;

以前本人都是用多个TextView拼起来,不仅感觉很蠢,操作起来也蛮恶心;

直到接触到了SpannableStringBuilder,感觉整个人都好了;

在我搭建界面布局,会有一些带String占位符的默认文字,如:"现在的气温是:%s","今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s.";

之后在获取到数据时,直接String.format(String target, String... data),就能在对应位置插入数据;

最近遇到一个插入的数据还要换成另一种颜色的需求,觉得这个需求应该比较常见,所以就写了个工具类;

  1. /**
  2. * TextView色彩工具类
  3. * Created by me on 2015-08-10.
  4. */
  5. public class TextViewColorUtil {
  6.  
  7. /**
  8. * 在文字内容为"xxxxx%sxxxx"(一个格式化占位符)或"xxxx%1$sxxxx%2$x......xxxx%n$sxxxx"时(多个格式化占位符),完成格式化同时,设置新加入的文字颜色,同时也能够设置原来文字的颜色;
  9. * <p/>
  10. * 注:请务必保证单个格式化时,使用%s占位符;多格式化时,使用%n$s占位符;
  11. * 占位符数必须和想填入的字符串数目一致;
  12. *
  13. * @param texts 如果可变参数长度为0,不做处理;如果文字长度为0,默认为""
  14. * @param defaultColorId R.color.xxx 如果不想改变默认颜色(冒号前的文字颜色),可以填null
  15. * @param newContentColorId R.color.xxx
  16. */
  17. public static void setSubColorText(Context context, TextView tv, Integer defaultColorId, int newContentColorId, String... texts) {
  18.  
  19. if (texts != null) {
  20. if (texts.length == 1) {//单格式化参数情况
  21. if (defaultColorId != null)//1.如果有设置改编默认文字颜色,给予改变
  22. tv.setTextColor(defaultColorId);
  23.  
  24. String text = texts[0];
  25. if (StringUtil.isEmpty(text))//2.如果文字内容为null或者长度0,默认其为""
  26. text = "";
  27.  
  28. String originText = tv.getText().toString();//3.格式化,记录添加的字符串的起止index
  29. int indexStart = originText.indexOf("%s");
  30. int indexEnd = indexStart + text.length();
  31. String foo = String.format(originText, text);
  32. tv.setText(foo);
  33.  
  34. if (indexEnd > indexStart) {//4.如果有必要换色,执行
  35. SpannableStringBuilder style = new SpannableStringBuilder(foo);
  36. style.setSpan(new ForegroundColorSpan(context.getResources().getColor(newContentColorId)), indexStart, indexEnd, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
  37. tv.setText(style);
  38. }
  39.  
  40. } else if (texts.length > 1) {//多格式化
  41. if (defaultColorId != null)//1.如果有设置改编默认文字颜色,给予改变
  42. tv.setTextColor(defaultColorId);
  43.  
  44. int[] indexesStart = new int[texts.length];
  45. int[] indexesEnd = new int[texts.length];
  46. String originText = tv.getText().toString();
  47.  
  48. for (int i = 0; i < texts.length; i++) {
  49. String text = texts[i];
  50. if (StringUtil.isEmpty(text)) {//2.如果文字内容为null或者长度0,默认其为""
  51. text = "";
  52. }
  53.  
  54. String regular = "%" + (i + 1) + "$s";//3.格式化,记录添加的字符串的起止index
  55. indexesStart[i] = originText.indexOf(regular);
  56. if (i > 0) {
  57. int indexFix = 0;
  58. for (int j = 0; j <= i - 1; j++) {
  59. String formerRegular = "%" + (j + 1) + "$s";
  60. indexFix += (indexesEnd[j] - indexesStart[j]) - formerRegular.length();
  61. }
  62. indexesStart[i] += indexFix;
  63. }
  64. indexesEnd[i] = indexesStart[i] + text.length();
  65. }
  66. String foo = String.format(originText, (Object[]) texts);
  67. tv.setText(foo);
  68. SpannableStringBuilder style = new SpannableStringBuilder(foo);
  69. for (int i = 0; i < texts.length; i++) {
  70. if (indexesEnd[i] > indexesStart[i])//4.如果有必要换色,执行
  71. style.setSpan(new ForegroundColorSpan(context.getResources().getColor(newContentColorId)), indexesStart[i], indexesEnd[i], Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
  72. }
  73. tv.setText(style);
  74. }
  75. }
  76. }
  77. }

Code

目前只有一种颜色,如果有每个插入的数据还要不同色彩的需求...也蛮好改的.

把传入的参数int newContentColorId换成int[] newContentColorIds然后稍微改改逻辑就ok啦.

>_>

就以之前的两个例子举例吧:

1.

  1. textView.setText("现在的气温是:%s");
  2. TextViewColorUtil.setSubColorText(MainActivity.this, textView, null, R.color.blue,"3°c");

"现在的气温是:%s"

2.

  1. tv.setText("今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s.");
  2. TextViewColorUtil.setSubColorText(MainActivity.this, tv, null, R.color.red, "晴", "22°c", "9°c", "47%", "19");

"今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s."

Android TextView设置多彩文字的更多相关文章

  1. android TextView 设置部分文字背景色和文字颜色

    通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色 public class MainActivity extends Activity { ...

  2. android TextView 设置部分文字背景色 和 文字颜色

    通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  3. TextView 设置部分文字颜色及点击事件SpannableString

    设置TextView中一部分文字的颜色及点击事件. SpannableString gotoMsgListStr = new SpannableString("消息列表"); go ...

  4. Android TextView 设置行间距

    Android系统中TextView默认显示中文时会比较紧凑,不是很美观.为了让每行保持一定的行间距,可以设置属性android:lineSpacingExtra或android:lineSpacin ...

  5. android textview改变部分文字的颜色和string.xml中文字的替换(转)

    转   :http://blog.csdn.net/ljz2009y/article/details/23878669 一:TextView组件改变部分文字的颜色: TextView textView ...

  6. Android TextView设置个别字体样式

    TextView进一步深化:       Textview 能够对其文字进行格式化.       通过查询资料,了解到格式化文字的方式主要分为两大类:  第一类:HTML标签格式化文字      代码 ...

  7. Android textview 设置不同的字体大小和颜色

    在实际应用中,需要将一个字符串已不同的颜色,字体显示出来.当然完全可以通过不同textview拼接出来.也可以通过一个textview来展示. 步骤如下: 1.定义不同style . 不妨如下定义2个 ...

  8. android TextView 设置字体大小

    package com.example.yanlei.yl4; import android.graphics.Color;import android.os.Bundle;import androi ...

  9. android textview 设置不同的颜色和大小

    1.定义不同的style <style name="approval_detail_info_style1"> <item name="android: ...

随机推荐

  1. 收藏夹里的js

    释放右键 javascript:(function(){var doc=document;var bd=doc.body;bd.onselectstart=bd.oncopy=bd.onpaste=b ...

  2. mysql 安装以及运行

    目录: http://www.fenby.com/courses/mysqlke-cheng-lian-zai/ 1.下载 2.配置 3.启动服务器 4.启用客户端并修改用户信息 1.mysql的下载 ...

  3. JSP自定义标签/自定义标签打包

    有这样一个业务需求: 当我们在编辑某个用户时,需要设置该用户的角色,在转到编辑页面时,就需要自动勾选上该用户已经选择的角色,如下图: 当我们点击编辑时,会查询用户详细信息,以及角色集合传到编辑页面. ...

  4. WPF中Grid布局

    WPF中Grid布局XMAl与后台更改,最普通的登录界面为例. <Grid Width="200" Height="100" > <!--定义 ...

  5. spring开发的总结

    1,当出现无法创建bean,( Error creating bean with name 'fileUploadService': Injection of resource dependencie ...

  6. Pycharm快捷方式

    PYCHARM的快捷方式 PyCharm3.0默认快捷键(翻译的)1.编辑(Editing)Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意 ...

  7. JAVA动手动脑异常处理

    1>请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutEx ...

  8. Jmeter性能测试入门(转)

    出处:http://www.cnblogs.com/by-dream/p/5611555.html Jmeter性能测试步骤 1. 添加线程组之后,先设置这两项: 2. 添加一个http请求 被测的u ...

  9. shell脚本连接、读写、操作mysql数据库实例

    本文介绍了如何在shell中读写mysql数据库.主要介绍了如何在shell 中连接mysql数据库,如何在shell中创建数据库,创建表,插入csv文件,读取mysql数据库,导出mysql数据库为 ...

  10. 杭电ACM1000

    #include <stdio.h> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { p ...