有时候可以看到一些文本承载了不同风格的样式,比如背景色、下划线、下标、附带表情图等等
其实这些都可以通过SpannableString来完成,通过为之设置不同的Span属性,使不同的风格样式组合在一起,然后再将文本信息传给TextView,就可以呈现出绚丽多彩的效果
主要使用到的是 SpannableString 对象的 setSpan 方法,方法签名如下所示:
public void setSpan(Object what, int start, int end, int flags);
参数说明:
- what 要设置的各种Span样式
- start 样式的起始位置,样式在该位置上起作用,索引从0开始
- end 样式的结束位置,样式在该位置上不起作用
- flags 设置当起始位置前和起始位置后如果再插入新字符时的动作,即是否对新插入的字符应用同样的样式
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE: 前后都不包括,即前后位置都不会应用同样的样式
- Spanned.SPAN_EXCLUSIVE_INCLUSIVE: 前面不会,后面会
- Spanned.SPAN_INCLUSIVE_EXCLUSIVE: 前面会,后面不会
- Spanned.SPAN_INCLUSIVE_INCLUSIVE: 前后都会
所有的示例代码如下所示:
private void foregroundColorSpan() {
SpannableString spannableString = new SpannableString("这是前景色");
TextView tv_foregroundColorSpan = (TextView) findViewById(R.id.tv_foregroundColorSpan);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#FF4081"));
spannableString.setSpan(foregroundColorSpan, 2, spannableString.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
tv_foregroundColorSpan.setText(spannableString);
}
private void backgroundColorSpan() {
SpannableString spannableString = new SpannableString("这是背景色");
TextView tv_backgroundColorSpan = (TextView) findViewById(R.id.tv_backgroundColorSpan);
BackgroundColorSpan colorSpan = new BackgroundColorSpan(Color.parseColor("#30cfff"));
spannableString.setSpan(colorSpan, 2, spannableString.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
tv_backgroundColorSpan.setText(spannableString);
}
private void relativeSizeSpan() {
SpannableString spannableString = new SpannableString("可以设置相对字体大小");
TextView tv_relativeSizeSpan = (TextView) findViewById(R.id.tv_relativeSizeSpan);
//相对于默认字体大小的倍数
RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.2f);
RelativeSizeSpan sizeSpan02 = new RelativeSizeSpan(1.4f);
RelativeSizeSpan sizeSpan03 = new RelativeSizeSpan(1.6f);
RelativeSizeSpan sizeSpan04 = new RelativeSizeSpan(1.8f);
spannableString.setSpan(sizeSpan01, 4, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan02, 5, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan03, 6, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan04, 7, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_relativeSizeSpan.setText(spannableString);
}
private void absoluteSizeSpan() {
SpannableString spannableString = new SpannableString("可以设置绝对字体大小");
TextView tv_absoluteSizeSpan = (TextView) findViewById(R.id.tv_absoluteSizeSpan);
AbsoluteSizeSpan sizeSpan01 = new AbsoluteSizeSpan(12, true);
AbsoluteSizeSpan sizeSpan02 = new AbsoluteSizeSpan(14, true);
AbsoluteSizeSpan sizeSpan03 = new AbsoluteSizeSpan(16, true);
AbsoluteSizeSpan sizeSpan04 = new AbsoluteSizeSpan(18, true);
spannableString.setSpan(sizeSpan01, 4, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan02, 5, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan03, 6, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan04, 7, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_absoluteSizeSpan.setText(spannableString);
}
private void typeface() {
SpannableString spannableString = new SpannableString("可以设置字体");
TextView tv_typeface = (TextView) findViewById(R.id.tv_typeface);
TypefaceSpan typefaceSpan = new TypefaceSpan("sans-serif");
spannableString.setSpan(typefaceSpan, 4, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_typeface.setText(spannableString);
}
private void strikethroughSpan() {
SpannableString spannableString = new SpannableString("设置删除线");
TextView tv_strikethroughSpan = (TextView) findViewById(R.id.tv_strikethroughSpan);
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 2, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_strikethroughSpan.setText(spannableString);
}
private void underlineSpan() {
SpannableString spannableString = new SpannableString("设置下划线");
TextView tv_underlineSpan = (TextView) findViewById(R.id.tv_underlineSpan);
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 2, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_underlineSpan.setText(spannableString);
}
private void superscriptSpan() {
SpannableString spannableString = new SpannableString("设置上标");
TextView tv_superscriptSpan = (TextView) findViewById(R.id.tv_superscriptSpan);
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
spannableString.setSpan(superscriptSpan, 2, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_superscriptSpan.setText(spannableString);
}
private void subscriptSpan() {
SpannableString spannableString = new SpannableString("设置下标");
TextView tv_subscriptSpan = (TextView) findViewById(R.id.tv_subscriptSpan);
SubscriptSpan subscriptSpan = new SubscriptSpan();
spannableString.setSpan(subscriptSpan, 2, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_subscriptSpan.setText(spannableString);
}
private void styleSpan() {
SpannableString spannableString = new SpannableString("设置各种不同的字体风格:叶应是叶");
TextView tv_styleSpan = (TextView) findViewById(R.id.tv_styleSpan);
StyleSpan bold = new StyleSpan(Typeface.BOLD);
StyleSpan italic = new StyleSpan(Typeface.ITALIC);
StyleSpan boldItalic = new StyleSpan(Typeface.BOLD_ITALIC);
spannableString.setSpan(bold, 12, 13, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(italic, 13, 14, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(boldItalic, 14, 16, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_styleSpan.setText(spannableString);
}
private void imageSpan() {
SpannableString spannableString = new SpannableString("添加表情图片 ");
TextView tv_imageSpan = (TextView) findViewById(R.id.tv_imageSpan);
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.icon);
drawable.setBounds(0, 0, 44, 44);
ImageSpan imageSpan = new ImageSpan(drawable);
spannableString.setSpan(imageSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_imageSpan.setText(spannableString);
}
private void clickableSpan() {
SpannableString spannableString = new SpannableString("这是可以点击的文本:叶应是叶");
TextView tv_clickableSpan = (TextView) findViewById(R.id.tv_clickableSpan);
MyClickableSpan clickableSpan = new MyClickableSpan();
spannableString.setSpan(clickableSpan, 10, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv_clickableSpan.setMovementMethod(LinkMovementMethod.getInstance());
tv_clickableSpan.setText(spannableString);
}
private void urlSpan() {
SpannableString spannableString = new SpannableString("这是可以跳转到指定网页的文本:叶应是叶");
TextView tv_urlSpan = (TextView) findViewById(R.id.tv_urlSpan);
URLSpan urlSpan = new URLSpan("https://github.com/leavesC");
spannableString.setSpan(urlSpan, 15, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_urlSpan.setMovementMethod(LinkMovementMethod.getInstance());
tv_urlSpan.setHighlightColor(Color.parseColor("#303F9F"));
tv_urlSpan.setText(spannableString);
}
private class MyClickableSpan extends android.text.style.ClickableSpan {
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.parseColor("#004081"));
//设置是否显示下划线
ds.setUnderlineText(false);
}
@Override
public void onClick(View widget) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
}
网友评论