SpannableString与String相似,是一种字符串类型,TextView可以直接设置Spannable为显示文本,不相同的是,SpannableString可以使用setSpan方法实现字符串各种形式风格的显示,并且可以设置指定的区间。
setSpan(Object what, int start, int end, int flags)方法需要用户输入四个参数,what表示设置的格式是什么,可以是前景色、背景色也可以是可点击的文本等等(URLSpan、ClickableSpan、BackgroundColorSpan、ForegroundColorSpan、MaskFilterSpan、AbsoluteSizeSpan、RelativeSizeSpan、ImageSpan、ScaleXSpan、StyleSpan、SubscriptSpan、SuperscriptSpan、TextAppearanceSpan、TypefaceSpan、RasterizerSpan、StrikethroughSpan、UnderlineSpan)。MaskFilterSpan可以实现模糊和浮雕效果,RasterizerSpan可以实现光栅效果。start表示需要设置格式的子字符串的起始下标,同理end表示终了下标,flags属性就有意思了,共有四种属性:
SpannableStringBuilder实现对SpannableString的一个拼接效果,append()方法可以实现各种风格效果的SpannableString拼接,非常实用。
实例代码
package com.mazaiting.spannablestring;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) this.findViewById(R.id.textView);
}
/**
* 设置前景色
*/
public void foreGroundColor(View view){
SpannableString spannableString = new SpannableString("设置文字的前景色为淡蓝色");
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE"));
spannableString.setSpan(foregroundColorSpan, 9, spannableString.length(),
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置背景色
*/
public void backGroundColor(View view){
SpannableString spannableString = new SpannableString("设置文字的背景色为浅绿色");
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#AC00FF30"));
spannableString.setSpan(backgroundColorSpan, 9, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置文字大小
*/
public void relativeSize(View view){
SpannableString spannableString = new SpannableString("万丈高楼平地起");
RelativeSizeSpan relativeSizeSpan1 = new RelativeSizeSpan(1.2f);
RelativeSizeSpan relativeSizeSpan2 = new RelativeSizeSpan(1.4f);
RelativeSizeSpan relativeSizeSpan3 = new RelativeSizeSpan(1.6f);
RelativeSizeSpan relativeSizeSpan4 = new RelativeSizeSpan(1.8f);
RelativeSizeSpan relativeSizeSpan5 = new RelativeSizeSpan(1.6f);
RelativeSizeSpan relativeSizeSpan6 = new RelativeSizeSpan(1.4f);
RelativeSizeSpan relativeSizeSpan7 = new RelativeSizeSpan(1.2f);
spannableString.setSpan(relativeSizeSpan1, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan2, 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan3, 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan4, 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan5, 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan6, 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(relativeSizeSpan7, 6, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置删除线
*/
public void strikeThrough(View view){
SpannableString spannableString = new SpannableString("为文字设置删除线");
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置下划线
*/
public void underLine(View view){
SpannableString spannableString = new SpannableString("为文字设置下划线");
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置上标
*/
public void superScript(View view){
SpannableString spannableString = new SpannableString("为文字设置上标");
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
spannableString.setSpan(superscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置下标
*/
public void subScript(View view){
SpannableString spannableString = new SpannableString("为文字设置下标");
SubscriptSpan subscriptSpan = new SubscriptSpan();
spannableString.setSpan(subscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置粗体斜体
*/
public void styleSpan(View view){
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_INCLUSIVE);
spannableString.setSpan(styleSpan_I, 8, 10, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 设置图片
*/
public void imageSpan(View view){
SpannableString spannableString = new SpannableString("在文本中添加表情(表情)");
Drawable drawable = getResources().getDrawable(R.mipmap.emotion);
drawable.setBounds(0, 0, 42, 42);
ImageSpan imageSpan = new ImageSpan(drawable);
spannableString.setSpan(imageSpan, 6, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
/**
* 点击开启新界面
*/
public void click(View view){
SpannableString spannableString = new SpannableString("为文字设置点击事件");
MyClickableSpan clickableSpan = new MyClickableSpan("http://www.jianshu.com/");
spannableString.setSpan(clickableSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
// 使用ClickableSpan的文本如果想真正实现点击作用,
// 必须为TextView设置setMovementMethod方法,否则没有点击相应,
// 至于setHighlightColor方法则是控制点击是的背景色。
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, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("content", content);
intent.putExtras(bundle);
startActivity(intent);
}
}
/**
* 网络链接
*/
public void urlSpan(View view) {
SpannableString spannableString = new SpannableString("为文字设置超链接");
URLSpan urlSpan = new URLSpan("http://www.jianshu.com/");
spannableString.setSpan(urlSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
}
}
注: 文章中涉及到的Activity即一个空的Activity, 读者可以可以新建一个空界面即可。