如果要使用一个TextView中实现多种样式,需要用SpannableStringBuilder设置不同的Span,这些Span用于实现Rich Text,比如粗体,斜体,前景色,背景色,字体大小,字体风格等等
先看看设置后的效果,以下文字显示是用一个TextView来实现的
要想实现上面的效果关键函数就是
/**
* Mark the specified range of text with the specified object.
* The flags determine how the span will behave when text is
* inserted at the start or end of the span's range.
*/
public void setSpan(Object what, int start, int end, int flags) {
setSpan(true, what, start, end, flags);
}
第一个参数:传入一个对象,这些对象可以为:
RelativeSizeSpan 相对字体大小
AbsoluteSizeSpan 绝对字体大小
BackgroundColorSpan 背景色
ForegroundColorSpan 前景色,字体颜色
UnderlineSpan 下划线
URLSpan 超链接
StrikethroughSpan 在文字中划线
StyleSpan 字体风格
第二个参数:开始位置
第三个参数:结束位置
第四个参数:用于控制行为,是一些常量,控制开始和结束的区间
更详细的可以参考:http://developer.android.com/intl/zh-cn/reference/android/text/Spanned.html
布局文件,定义一个TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在Activity中设置
package com.example.listviewitem;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.text.util.Linkify;
import android.widget.TextView;
public class TestRichText extends Activity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rich_text);
text = (TextView) findViewById(R.id.text);
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String str1 = "广东省/";
String str2 = "深圳市/";
String str3 = "南山区";
String str4 = "百度";
int start, end;
stringBuilder.append(str1);
start = 0;
end = str1.length();
stringBuilder.setSpan(new RelativeSizeSpan(4), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置相对字体大小,相对默认字体
stringBuilder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置前景色也就是字体颜色
stringBuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置文字样式
stringBuilder.append(str2);
start = end;
end += str2.length();
stringBuilder.setSpan(new RelativeSizeSpan(3), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(new ForegroundColorSpan(Color.GREEN), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(new StrikethroughSpan(), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 在文字上划线
stringBuilder.append(str3);
start = end;
end += str3.length();
stringBuilder.setSpan(new RelativeSizeSpan(2), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(new ForegroundColorSpan(Color.BLUE), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(new BackgroundColorSpan(Color.BLACK), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置背景色
stringBuilder.append(str4);
start = end;
end += str4.length();
stringBuilder.setSpan(new UnderlineSpan(), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置下划线
stringBuilder.setSpan(new URLSpan("http://www.baidu.com"), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置文字的超链接
stringBuilder.setSpan(new AbsoluteSizeSpan(40), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置绝对字体大小
text.setAutoLinkMask(Linkify.ALL);
text.setText(stringBuilder);
}
}
如果一行文字中有多种不同的属性,以上例子就可以用上了。