textView文本不同颜色
2010-06-04 17:44
302 查看
// 文本样式
EditText et = (EditText) findViewById(R.id.tv);
et.setText("Styling the content of an editText dynamically");
Spannable sp = et.getText();
sp.setSpan(new BackgroundColorSpan(Color.RED), 3, 8,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //3代表从第几个字符开始变颜色,注意第一个字符序号是0.
//8代表变色到第几个字符.
sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0, 7,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(sp);
java程序中动态改变TextView特定内容的Style或者实现更为复杂的效果,下边将使用Spannable介绍具体的实现方法,请参照以下代码设置:
01.// Get our EditText object.
02.EditText vw = (EditText)findViewById(R.id.text);
03.
04.// Set the EditText's text.
05.vw.setText("Italic, highlighted, bold.");
06.
07.// If this were just a TextView, we could do:
08.// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
09.// to force it to use Spannable storage so styles can be attached.
10.// Or we could specify that in the XML.
11.
12.// Get the EditText's internal text storage
13.Spannable str = vw.getText();
14.
15.// Create our span sections, and assign a format to each.
16.str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
17.str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
18.str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
和大家分享一下:
TextView tv;
tv.setText("Test");
Spannable span = (Spannable)tv.getText();
spn.setSpan(new BackgroundColor(#ffffffff), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ps:start和end表示你要高亮显示的子串在Test中的起始和结束位置。
go2android 发表于 2009-9-11 10:19
忘了一项,setText的时候要用setText("Text", TextView.BufferType.SPANNABLE);
EditText et = (EditText) findViewById(R.id.tv);
et.setText("Styling the content of an editText dynamically");
Spannable sp = et.getText();
sp.setSpan(new BackgroundColorSpan(Color.RED), 3, 8,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //3代表从第几个字符开始变颜色,注意第一个字符序号是0.
//8代表变色到第几个字符.
sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0, 7,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(sp);
java程序中动态改变TextView特定内容的Style或者实现更为复杂的效果,下边将使用Spannable介绍具体的实现方法,请参照以下代码设置:
01.// Get our EditText object.
02.EditText vw = (EditText)findViewById(R.id.text);
03.
04.// Set the EditText's text.
05.vw.setText("Italic, highlighted, bold.");
06.
07.// If this were just a TextView, we could do:
08.// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
09.// to force it to use Spannable storage so styles can be attached.
10.// Or we could specify that in the XML.
11.
12.// Get the EditText's internal text storage
13.Spannable str = vw.getText();
14.
15.// Create our span sections, and assign a format to each.
16.str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
17.str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
18.str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
和大家分享一下:
TextView tv;
tv.setText("Test");
Spannable span = (Spannable)tv.getText();
spn.setSpan(new BackgroundColor(#ffffffff), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ps:start和end表示你要高亮显示的子串在Test中的起始和结束位置。
go2android 发表于 2009-9-11 10:19
忘了一项,setText的时候要用setText("Text", TextView.BufferType.SPANNABLE);
相关文章推荐
- TextView显示(不同大小的,颜色)富文本
- android textview 文本显示不同颜色
- 如何在一个TextView中显示不同颜色的文本
- android TextView 文本不同颜色
- 富文本的使用之二 设置TextView不同字段的颜色和背景颜色
- Android中TextView显示不同颜色文本
- TextView中的文本加效果,如设置不同颜色,大小或点击事件
- 用一个textView实现不同颜色且多行的文本
- Android TextView富文本、显示不同的颜色
- TextView显示不同颜色字体的实现
- android textview 字体不同颜色
- android中TextView分段显示不同颜色,字体,时间
- TextView上面显示不同文字和颜色
- Android TextView 实现文字大小不同和文字颜色不同
- android中TextView同一行显示不同颜色文字
- 如何给textview中的不同的内容设置不同的颜色和字体
- textview设置不同格式字体颜色
- 关于一个Textview内的文字设置不同大小和颜色
- TextView中设置不同颜色字体
- android:TextView中的文本链接之--设置超链接、颜色、字体等属性