TextView组件改变部分文字的颜色和多字符串拼接
来源:互联网 发布:淘宝卖家登记表怎么刷 编辑:程序博客网 时间:2024/06/06 03:31
原文:http://www.cnblogs.com/bill-joy/archive/2012/03/24/2415539.html
一: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(newBackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色style.setSpan(newForegroundColorSpan(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文件参数:
<</SPAN>string name="baoxiang">您今天打了%1$d局,还差%2$d局可获得%3$s!</</SPAN>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(newForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);style.setSpan(newForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);style.setSpan(newBackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); textView.setText(style);
0 0
- TextView组件改变部分文字的颜色和多字符串拼接
- TextView组件改变部分文字的颜色和多字符串拼接
- TextView组件改变部分文字的颜色:
- TextView组件改变部分文字的颜色
- TextView组件改变部分文字的颜色
- android textview改变部分文字的颜色
- android textview改变部分文字的颜色
- 如何改变Textview的部分文字颜色
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- textview改变部分文字的颜色和string.xml中文字的替换
- Android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- 改变textview的部分字体和颜色
- 查看数据库登录用户
- 面经阅读
- 姿态解算中的欧垃角与四元数
- 封装与访问控制
- android ViewDragHelper详解
- TextView组件改变部分文字的颜色和多字符串拼接
- C++极客班学习笔记 一
- C++ vertor用法
- <JavaWeb-Servlet学习>Servlet连接数据库错误
- 制作帮助文档
- 1. 搭建工程
- Google Snappy string 压缩/解压缩(Java)
- Develop -- Training(十一) -- 通过NFC共享文件
- ExpandableListView