android textview改变部分文字的颜色和string.xml中文字的替换(转)
转 :http://blog.csdn.net/ljz2009y/article/details/23878669
一:TextView组件改变部分文字的颜色:
- TextView textView = (TextView)findViewById(R.id.textview);
- //方法一:
- textView.setText(Html.fromHtml("<font color=\"#ff0000\">红色</font>其它颜色"));
- //方法二:
- String text = "获得银宝箱!";
- SpannableStringBuilder style=new SpannableStringBuilder(text);
- style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色
- style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色
- textView.setText(style);
二:Android string.xml文件中的整型和string型代替:
- String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");
对应的string.xml文件参数:
- <string name="baoxiang">您今天打了%1$d局,还差%2$d局可获得%3$s!</string>
%1$d表达的意思是整个name=”baoxiang”字符串中,第一个整型
在项目开发者,经常需要把以上两者结合起来使用。可以避免很多textview的拼接,如下所示:
- TextView textView = (TextView)findViewById(R.id.testview);
- String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");
- int index[] = new int[3];
- index[0] = text.indexOf("2");
- index[1] = text.indexOf("18");
- index[2] = text.indexOf("银宝箱");
- SpannableStringBuilder style=new SpannableStringBuilder(text);
- style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- style.setSpan(new AbsoluteSizeSpan(30),index[0],index[1],index[1]+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //设置字体大小
- textView.setText(style);
android textview改变部分文字的颜色和string.xml中文字的替换(转)的更多相关文章
- 【我的Android进阶之旅】Android Studio如何轻松整理字符串到string.xml中
使用Android Studio一段时间了,还有很多小技巧没有掌握.比如:平常将字符串整理到string.xml中,都是手动的去复制字符串到string.xml中,然后再回来修改引用该字符串的代码,这 ...
- android中string.xml中%1$s、%1$d等的用法
今天在研究前辈写的代码的时候,突然发现string里面出现了<stringname="item_recent_photo">最近拍摄%1$s</string> ...
- Android TextView里显示两种颜色
今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码: TextView TV = (TextView)findViewById(R.id.mytextview01); S ...
- 【转】Android TextView SpannableStringBuilder 图文混排颜色斜体粗体下划线删除线
spannableStringBuilder 用法详解: SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:." ...
- iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicati ...
- Android Studio 轻松整理字符串到string.xml中
昨天了解了Alt+Enter快捷键的大用处,今天又发现了一个快捷的方法,必须记下来.转载请注明出处http://www.cnblogs.com/LT5505/p/5466630.html 1.首先代码 ...
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...
- Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...
- Android中点击按钮获取string.xml中内容并弹窗提示
场景 AndroidStudio跑起来第一个App时新手遇到的那些坑: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103797 ...
随机推荐
- MMORPG大型游戏设计与开发(part3 of net)
这一部分需要向大家介绍的是服务器的select以及收发包的具体流程,从核心代码功能上分析网络交互具体过程. 首先大家要看第二部分(part2 of net)的代码结构图,因为在接下来的流程过程中会用到 ...
- [Top-Down Approach]My First C/S Program [Python]
These days I was learning from Computer Networking --A Top-Down Approach by Kurose Ross. I modified ...
- 第9章 用内核对象进行线程同步(3)_信号量(semaphore)、互斥对象(mutex)
9.5 信号量内核对象(Semaphore) (1)信号量的组成 ①计数器:该内核对象被使用的次数 ②最大资源数量:标识信号量可以控制的最大资源数量(带符号的32位) ③当前资源数量:标识当前可用资源 ...
- 异常总结<经典例题>
public class Test1 { public static void main(String[] args) { try { add(1); System.out.println(" ...
- RFID基础知识
BS:BinarySearch. TSA:TimeSlottedAloha. BSA:基本二进制搜索算法. DBSA:动态二进制搜索算法. RBSA:后退式二进制搜索算法. FSA:Frame Slo ...
- iOS UIControl 详解
UIControl是UIView的子类,当然也是UIResponder的子类.UIControl是诸如UIButton,UISwitch,UItextField等控件的父类,它本身包含了一些属性和方法 ...
- 转- android硬件传感器
纯属转载:http://dev.10086.cn/cmdn/bbs/thread-41843-1-1.html 1.传感器入门 自从苹果公司在2007年发布第一代iPhone以来,以前看似和手机挨不着 ...
- CGPoint、CGSize、CGRect and UIView
首先要弄懂几个基本的概念. 一)三个结构体:CGPoint.CGSize.CGRect 1. CGPoint /* Points. */ struct CGPoint { CGFloat x; CGF ...
- PAT 1009. 说反话 (20) JAVA
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
- 判断百度某一经纬度的地图颜色值python
from PIL import Image import MySQLdb import os import urllib import time from multiprocessing.dummy ...