整理的一些 SpannableString 的简单的用法
PS: 我也是小白一个,写这篇文章是为了作为笔记记录,若有错,请指正。
SpannableString
SpannableString和String一样,都字符串类型,同样TextView也可以直接设置SpannableString作为显示文本,不同的是SpannableString可以通过使用其方法setSpan()方法实现字符串各种形式风格的显示,更可以为指定下标区间内的子字符串设置格式。
构造函数
SpannableString (CharSequence source)
setSpan
void setSpan (Object what, int start, int end, int flags)
/**
* what 需要设置的格式
* start 子字符串起始下标值
* end 子字符串结束下标值
* flags 下标取值标志,取值:
* Spanned.SPAN_INCLUSIVE_EXCLUSIVE 从起始下标到结束下标,包括起始下标
* Spanned.SPAN_INCLUSIVE_INCLUSIVE 从起始下标到结束下标,同时包括起始下标和结束下标
* Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 从起始下标到终了下标,但都不包括起始下标和结束下标
* Spanned.SPAN_EXCLUSIVE_INCLUSIVE 从起始下标到结束下标,包括结束下标
**/
常见格式(类/接口)
即void setSpan (Object what, int start, int end, int flags) 中的 what
ForegroundColorSpan
文本前景色,效果和TextView的setTextColor()类似.
SpannableString spannableString = new SpannableString("设置文字的前景色为蓝色");
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0000EE"));
spannableString.setSpan(colorSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
BackgroundColorSpan
文本背景色,效果和TextView的setBackground()类似.
SpannableString spannableString = new SpannableString("设置文字的背景色为淡绿色");
BackgroundColorSpan colorSpan = new BackgroundColorSpan(Color.parseColor("#AA00FF00"));
spannableString.setSpan(colorSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
RelativeSizeSpan
文字相对大小,在TextView原有的文字大小的基础上,相对设置文字大小.
SpannableString spannableString = new SpannableString("大小大");
RelativeSizeSpan sizeSpan0 = new RelativeSizeSpan(1.4f);
RelativeSizeSpan sizeSpan1 = new RelativeSizeSpan(0.8f);
RelativeSizeSpan sizeSpan2 = new RelativeSizeSpan(1.4f);
spannableString.setSpan(sizeSpan0, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan1, 1, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan2, 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
StrikethroughSpan
文本中划线,也就是常说的删除线.
SpannableString spannableString = new SpannableString("为文字设置删除线");
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
UnderlineSpan
文本下划线.
SpannableString spannableString = new SpannableString("为文字设置下划线");
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
SuperscriptSpan
上标
SpannableString spannableString = new SpannableString("为文字设置上标");
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
spannableString.setSpan(superscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
UnderlineSpan
文本下划线.
SpannableString spannableString = new SpannableString("为文字设置下划线");
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
SuberscriptSpan
下标
SpannableString spannableString = new SpannableString("为文字设置下标");
SuperscriptSpan suebrscriptSpan = new SuberscriptSpan();
spannableString.setSpan(suberscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
StyleSpan
文字显示风格(粗体、斜体),和TextView属性android:textStyle类似
SpannableString spannableString = new SpannableString("为文字设置粗体、斜体风格");
StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD);
StyleSpan styleSpan_I = new StyleSpan(Typeface.ITALIC);
spannableString.setSpan(styleSpan_B, 5, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(styleSpan_I, 8, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
ImageSpan
文本图片
SpannableString spannableString = new SpannableString("在文本中添加图片(图片)");
Drawable drawable = getResources().getDrawable(R.mipmap.a9c);
drawable.setBounds(0, 0, 42, 42);
ImageSpan imageSpan = new ImageSpan(drawable);
spannableString.setSpan(imageSpan, 6, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
ClickableSpan
可点击的文本,设置这个属性的文本可以响应用户点击事件,点击事件用户可以自定义.
SpannableString spannableString = new SpannableString("为文字设置点击事件");
MyClickableSpan clickableSpan = new MyClickableSpan("https://developer.android.com/reference/android/text/style/ClickableSpan.html");
spannableString.setSpan(clickableSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
/** 点击事件 **/
class MyClickableSpan extends ClickableSpan {
private String content;
public MyClickableSpan(String content) {
this.content = content;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
@Override
public void onClick(View widget) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
Bundle bundle = new Bundle();
bundle.putString("content", content);
intent.putExtra("bundle", bundle);
startActivity(intent);
}
}
/*
注意:使用ClickableSpan的文本如果想真正实现点击作用,必须为TextView设置setMovementMethod()方法,否则没有点击相应,setHighlightColor()方法是控制点击时的背景色。
*/
URLSpan
超链接文本
SpannableString spannableString = new SpannableString("为文字设置超链接");
URLSpan urlSpan = new URLSpan("https://developer.android.com/reference/android/text/style/URLSpan.html");
spannableString.setSpan(urlSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
SpannableStringBuilder
SpannableString 的长度是不可变的
SpannableStringBuilder 的长度是可变的
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("原来的");
SpannableString spannableString = new SpannableString(" 增加的");
spannableStringBuilder.append(ss1);
spannableStringBuilder.append("也可以是字符串");