SpannableString和SpannableStringBuilder的用法

1.SpannableString与SpannableStringBuilder简介

SpannableStringBuilder,SpannableString其实和String一样,都是一种字符串类型。不同的是SpannableString可以通过使用其方法setSpan方法实现字符串各种形式风格的显示。比如在原来String上加下划线、加背景色、改变字体颜色、用图片把指定的文字给替换掉,总之,SpannableString、SpannableStringBuilder与String一样,可以认为是String的升级版。SpannableString与SpannableStringBuilder区别就比如String和StringBuilder一样。

2.setSpan()

void setSpan (Object what, int start, int end, int flags)

object what :对应的各种Span,后面会提到;

int start:开始应用指定Span的位置,索引从0开始

int end:结束应用指定Span的位置,不包含

int flags

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE:前后都不包括(在标志位【start,end)前后添加文字,新添加的文字不会有任何设置的属性)

Spannable.SPAN_EXCLUSIVE_INCLUSIVE :前面不包括,后面包括。(在标志位【start,end)前添加文字,新添加的文字不会有任何设置的属性,后边的添加的文字会带有设置的what属性)

Spannable.SPAN_INCLUSIVE_EXCLUSIVE :前面包括,后面不包括。(在标志位【start,end)后添加文字,新添加的文字不会有任何设置的属性,前边边的添加的文字会带有设置的what属性)

Spannable.SPAN_INCLUSIVE_INCLUSIVE :前后都包括。前后都不包括(在标志位【start,end)前后添加文字,新添加的文字会有设置的属性)

3.object what 各种设置

3.1字体颜色(ForegroundColorSpan)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(Color.parseColor("#FF4040"));

    stringBuilder.setSpan(foregroundColorSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.2背景颜色(BackgroundColorSpan)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    //背景颜色

    BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(Color.parseColor("#FF4040"));

    stringBuilder.setSpan(backgroundColorSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.3文本可点击,有点击事件(ClickableSpan)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    ClickableSpan clickableSpan=new ClickableSpan() {

        @Override

        public void onClick(View widget) {

            Toast.makeText(MainActivity.this,"可点击",Toast.LENGTH_LONG).show();

        }

        @Override

        public void updateDrawState(TextPaint ds) {

            //去掉可点击文字的下划线

            ds.setUnderlineText(false);

        }

    };

    //文本可点击,有点击事件

    stringBuilder.setSpan(clickableSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // 设置此方法后,点击事件才能生效

    textView.setMovementMethod(LinkMovementMethod.getInstance());

    textView.setText(stringBuilder);

3.4 模糊效果(BlurMaskFilter)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    MaskFilter filter=new BlurMaskFilter(4.0f,BlurMaskFilter.Blur.OUTER);

    MaskFilterSpan maskFilterSpan=new MaskFilterSpan(filter);

    stringBuilder.setSpan(maskFilterSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.5 删除线效果 (StrikethroughSpan)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    stringBuilder.setSpan(new StrikethroughSpan(),0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setMovementMethod(LinkMovementMethod.getInstance());

    textView.setText(stringBuilder);

3.6 下划线效果(UnderlineSpan)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    UnderlineSpan un=new UnderlineSpan();

    //下划线效果

    stringBuilder.setSpan(un,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.7 字体绝对大小效果(AbsoluteSizeSpan)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    AbsoluteSizeSpan  ab=new AbsoluteSizeSpan(30,true);

    //文本字体绝对的大小

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.8 设置图片(DynamicDrawableSpan)基于底部

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    DynamicDrawableSpan ab=new DynamicDrawableSpan() {

        @Override

        public Drawable getDrawable() {

            Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);

            drawable.setBounds(0,0,50,50);

            return drawable;

        }

    };

    //文本字体绝对的大小

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.9 设置图片(ImageSpan )

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher_round);

    drawable.setBounds(0,0,100,100);

    ImageSpan ab=new ImageSpan(drawable);

    //文本字体绝对的大小

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.10 相对字体大小(RelativeSizeSpan )

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    RelativeSizeSpan ab=new RelativeSizeSpan(3.0f);

    //文本字体绝对的大小

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.10 基于X的缩放(ScaleXSpan )

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    ScaleXSpan ab=new ScaleXSpan(3.0f);

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.11 字体样式:粗体、斜体等(StyleSpan )

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    StyleSpan ab=new StyleSpan(Typeface.BOLD_ITALIC);

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.12 上标(SubscriptSpan )

    String content="22的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    SuperscriptSpan ab=new SuperscriptSpan();

    stringBuilder.setSpan(ab,1,2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.12 字体、大小、样式和颜色(TextAppearanceSpan)

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    //自定义文本样式

    TextAppearanceSpan ab=new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium);

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.13 文本字体(TypefaceSpan )

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    //文字字体

    TypefaceSpan ab=new TypefaceSpan ("serif");

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setText(stringBuilder);

3.13 文本超链接(URLSpan )

    String content="预祝党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    //文字字体

    URLSpan ab=new URLSpan  ("http://www.baidu.com");

    stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setMovementMethod(new LinkMovementMethod());

    textView.setText(stringBuilder);

3.14 下标(SubscriptSpan )

    String content="log10党的十九大完美谢慕";

    SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);

    SubscriptSpan ab=new SubscriptSpan();

    stringBuilder.setSpan(ab,3,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    textView.setMovementMethod(new LinkMovementMethod());

    textView.setText(stringBuilder);

---------------------

原文链接:https://blog.csdn.net/baidu_31956557/article/details/78339071

你可能感兴趣的:(SpannableString和SpannableStringBuilder的用法)