转   :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);

android textview改变部分文字的颜色和string.xml中文字的替换(转)的更多相关文章

  1. 【我的Android进阶之旅】Android Studio如何轻松整理字符串到string.xml中

    使用Android Studio一段时间了,还有很多小技巧没有掌握.比如:平常将字符串整理到string.xml中,都是手动的去复制字符串到string.xml中,然后再回来修改引用该字符串的代码,这 ...

  2. android中string.xml中%1$s、%1$d等的用法

    今天在研究前辈写的代码的时候,突然发现string里面出现了<stringname="item_recent_photo">最近拍摄%1$s</string> ...

  3. Android TextView里显示两种颜色

    今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码: TextView TV = (TextView)findViewById(R.id.mytextview01); S ...

  4. 【转】Android TextView SpannableStringBuilder 图文混排颜色斜体粗体下划线删除线

    spannableStringBuilder 用法详解: SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:." ...

  5. iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)

                      #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicati ...

  6. Android Studio 轻松整理字符串到string.xml中

    昨天了解了Alt+Enter快捷键的大用处,今天又发现了一个快捷的方法,必须记下来.转载请注明出处http://www.cnblogs.com/LT5505/p/5466630.html 1.首先代码 ...

  7. 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片

    一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...

  8. Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片

    一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...

  9. Android中点击按钮获取string.xml中内容并弹窗提示

    场景 AndroidStudio跑起来第一个App时新手遇到的那些坑: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103797 ...

随机推荐

  1. CPU和GPU性能对比

    计算20000次10000点的fft,分别使用CPU和GPU,得 the running time of cpu is : 2.3696s the running time of gpu is : 0 ...

  2. 【HTML】嵌入图像

    img元素允许我们在HTML文档里嵌入图像. 要嵌入一张图像需要使用src和alt属性,代码如下: <img src="../img/example/img-map.jpg" ...

  3. 编写 Unity Editor 插件

    Editor Style Viewer 在开发过程中,我喜欢编写一些辅助的Editor插件,方便在游戏开发过程进行调试. 下面是摘自Asset Store的一个查看Unity 默认GUI样式的小工具 ...

  4. 百度搜索--jquery

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. TestLink测试软件安装条件检查不通过的解决方案

    在第一次安装的时候出现这个错误信息 解决办法: 修改config.inc.php文件里的两个属性值为: $tlCfg->log_path = TL_ABS_PATH . 'logs' . DIR ...

  6. PhpExcel中文帮助手册|PhpExcel使用方法

    下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...

  7. Pycharm: keyboard reference

    Source: Official set ♥ Editing Ctrl + Space Basic code completion (the name of any class, method or ...

  8. dotnetGen 系列终于开源了

    https://github.com/2881099/dotnetGen .Net 3.0 + SqlServer 生成器 https://github.com/2881099/dotnetGen_s ...

  9. 如何把Json格式字符写进text文件中

    本篇一步一步学习怎样把显示于网页的json格式的字符串写进text文件中,并保存起来.学习到创建model, Entity, 序列化List<object>转换为json,显示于网页上.然 ...

  10. salt yum安装lamp

    在批量安装软件前,先找台测试机yum装一遍,看是否报错等,是否依赖包全等 .         本次我们在dev环境下搞. 先看一下已搞成功的目录结构         定义dev环境的第二个好处     ...