android textview改变部分文字的颜色和string.xml中文字的替换
来源:互联网 发布:萨博尼斯数据 编辑:程序博客网 时间:2024/05/22 09:04
项目中做国际化UI的调整,因为中文和英文不一样,所以,用到了这里的知识:
- 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);
- textView.setText(style);
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- 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中文字的替换
- android textview改变部分文字的颜色
- android textview改变部分文字的颜色
- TextView组件改变部分文字的颜色:
- TextView组件改变部分文字的颜色
- TextView组件改变部分文字的颜色
- 如何改变Textview的部分文字颜色
- Android 关于TextView中,设置指定部分文字改变颜色,和指定部分文字点击事件
- ruby学习笔记(三)--ruby methods
- POJ 1556 线段相交
- 有关时间复杂度
- 在javaSE工程中初始化Spring并取得bean .
- 11年千橡互动(人人网)C++笔试题
- android textview改变部分文字的颜色和string.xml中文字的替换
- 教您如何实现SQL Server日期转换
- HTTP协议
- 路过
- 爱恨交加的openmeetings2.0
- java.lang.RuntimeException: Unable to start activity ComponentInfo解决方案
- 从此不再受伤害,不再说爱_伤感日志
- require(),include(),require_once()和include_once()区别
- iOS6 Grouped类型UITableView背景色设置问题