转   :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. Use getopt() & getopt_long() to Parse Arguments

    Today I came across a function [getopt] by accident. It is very useful to parse command-line argumen ...

  2. JAVA爬虫挖取CSDN博客文章

    开门见山,看看这个教程的主要任务,就去csdn博客,挖取技术文章,我以<第一行代码–安卓>的作者为例,将他在csdn发表的额博客信息都挖取出来.因为郭神是我在大学期间比较崇拜的对象之一.他 ...

  3. POJ3494Largest Submatrix of All 1’s[单调栈]

    Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5883   Ac ...

  4. poj[2392]space elevator

    Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...

  5. jquery的工具方法isFunction/isArray/isWindow/isNumeric/isPlainObject/isEmptyObject

    isFunction : 是否函数 isArray : 是否数组 isWindow : 是否window isNumeric : 是否数字 type : 数据类型方法 isPlainObject : ...

  6. SPM paired t-test步骤

    首先感谢大神空里流霜耐心的讲解,这篇笔记内容主要是整理他的谆谆教导,虽然他也看不到>< 所有数据都要经过平滑. Paired t-test虽然在2nd-level analysis中,但是 ...

  7. removeNode is not defined removeNode is not a function

    在javascript操作dom树的时候可能会经常遇到增加,删除节点的事情,比如一个输入框后一个增加按钮,一个删除按钮,点击增加就增加 个输入框,点击删除就删除对应的输入框.在一些js框架,如Prot ...

  8. tomcat启动时报错

    http://www.oschina.net/question/1162040_229925?sort=time 解决:

  9. [xen]XenServer6.2增加第二块盘&vm开启自动启动&图形化安装centos

    很多服务器都会多块盘或者做了Raid的多个虚拟磁盘,而安装xenserver后,他只会默认挂载第一快盘,也就是安装xenServer系统的那块. 为XenServer6.2挂载/增加第二块硬盘的方法 ...

  10. codevs2806 红与黑

    难度等级:白银 codevs2806 红与黑 题目描述 Description 有一个矩形房间,覆盖正方形瓷砖.每块瓷砖涂成了红色或黑色.一名男子站在黑色的瓷砖上,由此出发,可以移到四个相邻瓷砖之一, ...