android textview改变部分文字的颜色和string.xml中文字的替换
2013-11-21 13:56
531 查看
一:TextView组件改变部分文字的颜色:
1.TextView textView = (TextView)findViewById(R.id.textview);
2.
3.//方法一:
4.textView.setText(Html.fromHtml("<font color=\"#ff0000\">红色</font>其它颜色"));
5.
6.//方法二:
7. String text = "获得银宝箱!";
8. SpannableStringBuilder style=new SpannableStringBuilder(text);
9. style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色
10. style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色
11. 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=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一个整型%1$d表达的意思是整个name=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一个整型
在项目开发者,经常需要把以上两者结合起来使用。可以避免很多textview的拼接,如下所示:
1.TextView
textView = (TextView)findViewById(R.id.testview);
2.
3.String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");
4. int index[] = new int[3];
5. index[0] = text.indexOf("2");
6. index[1] = text.indexOf("18");
7. index[2] = text.indexOf("银宝箱");
8.
9. SpannableStringBuilder style=new SpannableStringBuilder(text);
10. style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
11. style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
12. style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
13. textView.setText(style);
原文地址:/article/11827747.html
1.TextView textView = (TextView)findViewById(R.id.textview);
2.
3.//方法一:
4.textView.setText(Html.fromHtml("<font color=\"#ff0000\">红色</font>其它颜色"));
5.
6.//方法二:
7. String text = "获得银宝箱!";
8. SpannableStringBuilder style=new SpannableStringBuilder(text);
9. style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色
10. style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色
11. 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=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一个整型%1$d表达的意思是整个name=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一个整型
在项目开发者,经常需要把以上两者结合起来使用。可以避免很多textview的拼接,如下所示:
1.TextView
textView = (TextView)findViewById(R.id.testview);
2.
3.String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");
4. int index[] = new int[3];
5. index[0] = text.indexOf("2");
6. index[1] = text.indexOf("18");
7. index[2] = text.indexOf("银宝箱");
8.
9. SpannableStringBuilder style=new SpannableStringBuilder(text);
10. style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
11. style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
12. style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
13. textView.setText(style);
原文地址:/article/11827747.html
相关文章推荐
- 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中文字的替换及部分内容设置颜色、字体、超链接、图片
- Android textview改变部分文字的颜色和string.xml中文字的替换
- 【Android基础】textview改变一部分文字的颜色和string.xml中文字的替换
- textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色
- android TextView 首行缩进与部分文字改变颜色大小效果
- Android 关于TextView中,设置指定部分文字改变颜色,和指定部分文字点击事件
- android textview改变部分文字的颜色
- Android利用SpannableStringBuilder设置TextView中部分文字的颜色...
- android-修改TextView中部分文字的颜色
- android--设置TextView部分文字的颜色和背景(高亮显示)
- Textview中部分文字改变大小颜色的方法
- android 各种 span的生成 如:修改TextView中部分文字的颜色 ,生成图片span