SpannableString实现富文本效果

spannableString.png

以上展示图。
都是用SpannableString实现的,在这个基础上可以实现一些类似富文本消息的编辑。

1:超链接

image.png
实现代码:URLSpan
SpannableString spannableString = new SpannableString("为文字设置超链接");
        URLSpan urlSpan = new URLSpan("http://www.jianshu.com/u/b216fb12a05d");
        spannableString.setSpan(urlSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        hypeLinkText.setMovementMethod(LinkMovementMethod.getInstance());
        hypeLinkText.setHighlightColor(Color.parseColor("#36969696"));
        hypeLinkText.setText(spannableString);

2:前景色

image.png
实现代码:ForegroundColorSpan
 SpannableString spannableString = new SpannableString("这是文字的前景色");
        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#0099ee"));
        spannableString.setSpan(span,5,8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        foreginColorText.setText(spannableString);

3:背景色

image.png
实现代码 BackgroundColorSpan
 SpannableString spannableString1 = new SpannableString("设置文字的背景色");
        BackgroundColorSpan span1 = new BackgroundColorSpan(Color.parseColor("#ac00ff30"));
        spannableString1.setSpan(span1,5,8,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        backColorText.setText(spannableString1);

4:不同字体大小

image.png
实现代码 RelativeSizeSpan
 SpannableString spannableString2 = 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);
        RelativeSizeSpan relativeSizeSpan8 = new RelativeSizeSpan(1.2f);

        spannableString2.setSpan(relativeSizeSpan1,1,2,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(relativeSizeSpan2,2,3,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(relativeSizeSpan3,3,4,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(relativeSizeSpan4,4,5,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(relativeSizeSpan5,5,6,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(relativeSizeSpan6,6,7,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(relativeSizeSpan7,7,8,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(relativeSizeSpan8,8,9,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        differentTextSize.setText(spannableString2);

5:下划线

image.png
实现代码 UnderlineSpan
 SpannableString underlineString = new SpannableString("我是下划线下划线");
        UnderlineSpan underlineSpan = new UnderlineSpan();
        underlineString.setSpan(underlineSpan,2,8,Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        underlineText.setText(underlineString);

6:删除线

image.png
实现代码 StrikethroughSpan
SpannableString strikeSpannableString = new SpannableString("我是删除线");
        StrikethroughSpan StrikeSpan = new StrikethroughSpan();
        strikeSpannableString.setSpan(StrikeSpan,2,5,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        strikeText.setText(strikeSpannableString);

7:上标以及字体缩小

image.png
实现代码 SuperscriptSpan
SpannableString superString = new SpannableString("这个是上标");
        SuperscriptSpan span = new SuperscriptSpan();
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(0.6f);
        superString.setSpan(span,3,5, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        superString.setSpan(sizeSpan,3,5,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        superText.setText(superString);

8:下标以及字体缩小

image.png
实现代码 SubscriptSpan
 SpannableString spannablestring = new SpannableString("这个是下标");
        SubscriptSpan subSpan = new SubscriptSpan();
        spannablestring.setSpan(subSpan,3,5,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(0.6f);
        spannablestring.setSpan(sizeSpan,3,5,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        subScriptText.setText(spannablestring);

9:文字包含表情

image.png
实现代码 ImageSpan
 SpannableString imageString = new SpannableString("这个文字里面包含表情(表情)");
        Drawable image = getResources().getDrawable(R.mipmap.smiley_0);
        image.setBounds(0,0,50,50);
        ImageSpan imageSpan = new ImageSpan(image);
        imageString.setSpan(imageSpan,8,10,Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        containsImageText.setText(imageString);

你可能感兴趣的:(SpannableString实现富文本效果)