Android TextView设置多彩文字
在应用开发中时常会遇到需要在一段文字中插入几个不一样颜色文字的需求;
以前本人都是用多个TextView拼起来,不仅感觉很蠢,操作起来也蛮恶心;
直到接触到了SpannableStringBuilder,感觉整个人都好了;
在我搭建界面布局,会有一些带String占位符的默认文字,如:"现在的气温是:%s","今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s.";
之后在获取到数据时,直接String.format(String target, String... data),就能在对应位置插入数据;
最近遇到一个插入的数据还要换成另一种颜色的需求,觉得这个需求应该比较常见,所以就写了个工具类;
/**
* TextView色彩工具类
* Created by me on 2015-08-10.
*/
public class TextViewColorUtil { /**
* 在文字内容为"xxxxx%sxxxx"(一个格式化占位符)或"xxxx%1$sxxxx%2$x......xxxx%n$sxxxx"时(多个格式化占位符),完成格式化同时,设置新加入的文字颜色,同时也能够设置原来文字的颜色;
* <p/>
* 注:请务必保证单个格式化时,使用%s占位符;多格式化时,使用%n$s占位符;
* 占位符数必须和想填入的字符串数目一致;
*
* @param texts 如果可变参数长度为0,不做处理;如果文字长度为0,默认为""
* @param defaultColorId R.color.xxx 如果不想改变默认颜色(冒号前的文字颜色),可以填null
* @param newContentColorId R.color.xxx
*/
public static void setSubColorText(Context context, TextView tv, Integer defaultColorId, int newContentColorId, String... texts) { if (texts != null) {
if (texts.length == 1) {//单格式化参数情况
if (defaultColorId != null)//1.如果有设置改编默认文字颜色,给予改变
tv.setTextColor(defaultColorId); String text = texts[0];
if (StringUtil.isEmpty(text))//2.如果文字内容为null或者长度0,默认其为""
text = ""; String originText = tv.getText().toString();//3.格式化,记录添加的字符串的起止index
int indexStart = originText.indexOf("%s");
int indexEnd = indexStart + text.length();
String foo = String.format(originText, text);
tv.setText(foo); if (indexEnd > indexStart) {//4.如果有必要换色,执行
SpannableStringBuilder style = new SpannableStringBuilder(foo);
style.setSpan(new ForegroundColorSpan(context.getResources().getColor(newContentColorId)), indexStart, indexEnd, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv.setText(style);
} } else if (texts.length > 1) {//多格式化
if (defaultColorId != null)//1.如果有设置改编默认文字颜色,给予改变
tv.setTextColor(defaultColorId); int[] indexesStart = new int[texts.length];
int[] indexesEnd = new int[texts.length];
String originText = tv.getText().toString(); for (int i = 0; i < texts.length; i++) {
String text = texts[i];
if (StringUtil.isEmpty(text)) {//2.如果文字内容为null或者长度0,默认其为""
text = "";
} String regular = "%" + (i + 1) + "$s";//3.格式化,记录添加的字符串的起止index
indexesStart[i] = originText.indexOf(regular);
if (i > 0) {
int indexFix = 0;
for (int j = 0; j <= i - 1; j++) {
String formerRegular = "%" + (j + 1) + "$s";
indexFix += (indexesEnd[j] - indexesStart[j]) - formerRegular.length();
}
indexesStart[i] += indexFix;
}
indexesEnd[i] = indexesStart[i] + text.length();
}
String foo = String.format(originText, (Object[]) texts);
tv.setText(foo);
SpannableStringBuilder style = new SpannableStringBuilder(foo);
for (int i = 0; i < texts.length; i++) {
if (indexesEnd[i] > indexesStart[i])//4.如果有必要换色,执行
style.setSpan(new ForegroundColorSpan(context.getResources().getColor(newContentColorId)), indexesStart[i], indexesEnd[i], Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
tv.setText(style);
}
}
}
}
Code
目前只有一种颜色,如果有每个插入的数据还要不同色彩的需求...也蛮好改的.
把传入的参数int newContentColorId换成int[] newContentColorIds然后稍微改改逻辑就ok啦.
>_>
就以之前的两个例子举例吧:
1.
textView.setText("现在的气温是:%s");
TextViewColorUtil.setSubColorText(MainActivity.this, textView, null, R.color.blue,"3°c");
"现在的气温是:%s"
2.
tv.setText("今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s.");
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设置多彩文字的更多相关文章
- android TextView 设置部分文字背景色和文字颜色
通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色 public class MainActivity extends Activity { ...
- android TextView 设置部分文字背景色 和 文字颜色
通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- TextView 设置部分文字颜色及点击事件SpannableString
设置TextView中一部分文字的颜色及点击事件. SpannableString gotoMsgListStr = new SpannableString("消息列表"); go ...
- Android TextView 设置行间距
Android系统中TextView默认显示中文时会比较紧凑,不是很美观.为了让每行保持一定的行间距,可以设置属性android:lineSpacingExtra或android:lineSpacin ...
- android textview改变部分文字的颜色和string.xml中文字的替换(转)
转 :http://blog.csdn.net/ljz2009y/article/details/23878669 一:TextView组件改变部分文字的颜色: TextView textView ...
- Android TextView设置个别字体样式
TextView进一步深化: Textview 能够对其文字进行格式化. 通过查询资料,了解到格式化文字的方式主要分为两大类: 第一类:HTML标签格式化文字 代码 ...
- Android textview 设置不同的字体大小和颜色
在实际应用中,需要将一个字符串已不同的颜色,字体显示出来.当然完全可以通过不同textview拼接出来.也可以通过一个textview来展示. 步骤如下: 1.定义不同style . 不妨如下定义2个 ...
- android TextView 设置字体大小
package com.example.yanlei.yl4; import android.graphics.Color;import android.os.Bundle;import androi ...
- android textview 设置不同的颜色和大小
1.定义不同的style <style name="approval_detail_info_style1"> <item name="android: ...
随机推荐
- tomcat-maven-plugin
tomcat-maven-plugin , 我们可以使用这个插件把web应用一键式的部署到一个远程的tomcat中. 步骤1 : 修改 tomcat/conf/tomcat-users.xml ...
- 关于ADO.NET 超时的问题
前几天超时问题困扰我很头疼. 为什么我设置了链接字符串的超时时间很长,可是等了一小会就报错Timeout了? connectionString="Data Source=.;Initial ...
- 6、HTML5表单提交和PHP环境搭建
---恢复内容开始--- 1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>he ...
- 【Python扩展阅读【转】EasyGui 学习文档【超详细中文版】】
翻译改编自官方文档:http://easygui.sourceforge.net/tutorial/index.html 翻译改编者:小甲鱼,本文欢迎转载,转载请保证原文的完整性! 演示使用 Pyth ...
- python网络编程【四】(域名系统)
域名系统(DNS)是一个分布式的数据库,它主要用来把主机名转换成IP地址.DNS以及相关系统之所以存在,主要有以下两个原因: (1).它们可以使人们比较容易地记住名字. (2).它允许服务器改变IP地 ...
- Retrofit源码研究
2016-05-06 15:35:27 最近抽空研究了一下Retrofit源码,包括API使用.源码结构.使用到的设计模式.SDK的架构设计.作者设计/实现思路等,会形成一系列文章. 以前Retrof ...
- node.js中buffer需要知道的一些点
本文为阅读朴灵大大的<深入浅出node.js>笔记: 在前端开发的时候,我们不曾用过buffer,也没得用.buffer是node环境引入的,用来方便应对二进制数据的处理.这里我们对它应该 ...
- 痛苦的vsftpd配置
1.下载安装:yum install vsftpd 2.添加用户和组(不一定要添加组) group -g 1010 customedname useradd -g customedname -d /h ...
- mysql使用
1.以查询结果建表 create table newTableName select column1 [newName1] [, column2 [newName2], .. , columnn [n ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...