SpannableString spannableString =
new SpannableString(
"前景色背景色相对大小删除线下划线" +
"上标小上标下标粗体斜体显示图片点击超链接");
//设置文本前景颜色
ForegroundColorSpan foregroundColorSpan =
new ForegroundColorSpan(Color.parseColor(
"#0099EE"));
//
文本设置文本背景色
BackgroundColorSpan backgroundColorSpan =
new BackgroundColorSpan(Color.parseColor(
"#AC00FF30"));
//为文本设置相对大小,在TextView原有的文字大小的基础上,相对设置文字大小
RelativeSizeSpan relativeSizeSpan =
new RelativeSizeSpan(
2f);
//
为文本设置中划线,也就是常说的删除线
StrikethroughSpan strikethroughSpan =
new StrikethroughSpan();
//
下划线
UnderlineSpan underlineSpan =
new UnderlineSpan();SuperscriptSpan superscriptSpan =
new SuperscriptSpan();
//
为文本设置相对大小,在TextView原有的文字大小的基础上,相对设置文字大小
RelativeSizeSpan relativeSizeSpan2 =
new RelativeSizeSpan(
0.5f);
//
为文本设置下标
SubscriptSpan subscriptSpan =
new SubscriptSpan();
//
风格,粗体
StyleSpan styleSpan_B =
new StyleSpan(Typeface.BOLD);
//
风格,斜体
StyleSpan styleSpan_I =
new StyleSpan(Typeface.ITALIC);
//
将文本替换为图片
ImageSpan imageSpan =
new ImageSpan(
this, R.mipmap.ic_launcher);
//
设置点击事件
ClickableSpan clickableSpan =
new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(SpannableActivity.
this,
"点击", Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(
false);
}
};
//
超链接
URLSpan urlSpan =
new URLSpan(
"http://www.sdwfqin.com");
//
把各种处理的字符串添加到文本。
spannableString.setSpan(foregroundColorSpan,
0,
3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundColorSpan,
3,
6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan,
6,
10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(strikethroughSpan,
10,
13, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(underlineSpan,
13,
16, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(superscriptSpan,
16,
21, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan2,
18,
21, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(subscriptSpan,
21,
23, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(styleSpan_B,
23,
25, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(styleSpan_I,
25,
27, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(imageSpan,
29,
31, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(clickableSpan,
31,
33, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(urlSpan,
33,
36, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spanString.setMovementMethod(LinkMovementMethod.getInstance());
spanString(
你需要编辑的文本).setText(spannableString);