转   :http://blog.csdn.net/ljz2009y/article/details/23878669

一:TextView组件改变部分文字的颜色:

  1. TextView textView = (TextView)findViewById(R.id.textview);
  2. //方法一:
  3. textView.setText(Html.fromHtml("<font color=\"#ff0000\">红色</font>其它颜色"));
  4. //方法二:
  5. String text = "获得银宝箱!";
  6. SpannableStringBuilder style=new SpannableStringBuilder(text);
  7. style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置textview的背景颜色
  8. style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
  9. textView.setText(style);

二:Android string.xml文件中的整型和string型代替:

  1. String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");

对应的string.xml文件参数:

  1. <string name="baoxiang">您今天打了%1$d局,还差%2$d局可获得%3$s!</string>

%1$d表达的意思是整个name=”baoxiang”字符串中,第一个整型

在项目开发者,经常需要把以上两者结合起来使用。可以避免很多textview的拼接,如下所示:

  1. TextView textView = (TextView)findViewById(R.id.testview);
  2. String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");
  3. int index[] = new int[3];
  4. index[0] = text.indexOf("2");
  5. index[1] = text.indexOf("18");
  6. index[2] = text.indexOf("银宝箱");
  7. SpannableStringBuilder style=new SpannableStringBuilder(text);
  8. style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
  9. style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
  10. style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
  11. style.setSpan(new AbsoluteSizeSpan(30),index[0],index[1],index[1]+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);     //设置字体大小
  12. textView.setText(style);

最新文章

  1. python爬虫学习(10) —— 专利检索DEMO
  2. Drools 查询学习
  3. Seajs教程 配置文档
  4. leetcode 217
  5. uC/OS-II核心(Os_core)块
  6. 递归神经网络(RNN)简介(转载)
  7. 无锁编程(四) - CAS与ABA问题
  8. GodSon Easyui 结合Pluplaod插件的文件分割上传
  9. The 6th Zhejiang Provincial Collegiate Programming Contest-&gt;ProblemK:K-Nice
  10. 对象创建型模式------Builder(生成器)
  11. Flask源码解读(一)
  12. 网络编程(UDP协议-聊天程序)
  13. 在centos上面安装phantomjs
  14. win10 solidity开发环境搭建
  15. Spring:AOP
  16. FastDFS与springboot整合例子
  17. SpagoBI 教程 Lesson 1:Introduction and Installation
  18. Qt 线程基础
  19. LintCode题解
  20. 前端(四):JavaScript面向对象之自定义对象

热门文章

  1. 手机打开PDF文档中文英文支持(乱码问题)解决攻略
  2. 探索 OpenStack 之(17):计量模块 Ceilometer 中的数据收集机制
  3. CSS样式----图文详解:css样式表和选择器
  4. [tem]高精度2
  5. NOIP2015子串[序列DP]
  6. Editor扩展之查看Prefab用在那儿
  7. ActiveMQ初体验
  8. java 22 - 19 多线程之生产者和消费者的代码优化
  9. poj 2481
  10. 12个JQuery小贴士