TextView使用SpannableString设置复合文本

TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式、事件方面的设置。Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能:

1、BackgroundColorSpan 背景色 
2、ClickableSpan 文本可点击,有点击事件
3、ForegroundColorSpan 文本颜色(前景色)
4、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
5、MetricAffectingSpan 父类,一般不用
6、RasterizerSpan 光栅效果
7、StrikethroughSpan 删除线(中划线)
8、SuggestionSpan 相当于占位符
9、UnderlineSpan 下划线
10、AbsoluteSizeSpan 绝对大小(文本字体)
11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。
12、ImageSpan 图片
13、RelativeSizeSpan 相对大小(文本字体)
14、ReplacementSpan 父类,一般不用
15、ScaleXSpan 基于x轴缩放
16、StyleSpan 字体样式:粗体、斜体等
17、SubscriptSpan 下标(数学公式会用到)
18、SuperscriptSpan 上标(数学公式会用到)
19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
20、TypefaceSpan 文本字体
21、URLSpan 文本超链接

这么多文字,为了让你有看下去的勇气,下面来张效果图:
TextView+SpannableString设置复合文本


1、BackgroundColorSpan 背景色

SpannableString spanText = new SpannableString("萝卜白菜的博客 -- http://orgcent.com");
spanText.setSpan(new BackgroundColorSpan(Color.GREEN)0, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

2、ClickableSpan 文本可点击,有点击事件

Android系统默认把网址、电话、地图(geo地址)、邮箱等转换为超链接。

具体请查看android:TextView设置文本样式和超链接

和HTML中的一样,默认超链接都带下划线的,下面的方案可以在TextView中去掉超链接的下划线:

1、重写ClickableSpan类来去掉下划线样式(系统默认使用ClickableSpan来封装超链接)

//无下划线超链接,使用textColorLink、textColorHighlight分别修改超链接前景色和按下时的颜色
private class NoLineClickSpan extends ClickableSpan { 
    String text;

    public NoLineClickSpan(String text) {
        super();
        this.text = text;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(ds.linkColor);
        ds.setUnderlineText(false); <span style="color: red;">//去掉下划线</span>
    }

    @Override
    public void onClick(View widget) { 
        processHyperLinkClick(text); <span style="color: red;">//点击超链接时调用</span>
    }
}


2、把超链接文本封装为NoLineClickSpan对象,并添加到TextView中

TextView tv = findViewById(R.id.tv_click);
SpannableString spStr = new SpannableString("萝卜白菜博客--&gt;http://orgcent.com");
ClickSpan clickSpan = new NoLineClickSpan(vo); //设置超链接
spStr.setSpan(clickSpan, 0, str.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append(spStr);
tv.setMovementMethod(LinkMovementMethod.getInstance());

PS:不用把TextView的属性autoLink设为”all”.

3、设置超链接为可点击状态

tv.setMovementMethod(LinkMovementMethod.getInstance());

PS:在NoLineClickSpan类中实现onClick()回调方法.

3、ForegroundColorSpan 文本颜色(前景色)

spanText = new SpannableString("萝卜白菜的博客 -- http://orgcent.com");
spanText.setSpan(new ForegroundColorSpan(Color.BLUE)6, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

4、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
spanText = new SpannableString("MaskFilterSpan -- http://orgcent.com");
int length = spanText.length();
//模糊(BlurMaskFilter)
MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, Blur.OUTER));
spanText.setSpan(maskFilterSpan, 0, length - 10, Spannable.
SPAN_INCLUSIVE_EXCLUSIVE);
//浮雕(EmbossMaskFilter)
maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 83));
spanText.setSpan(maskFilterSpan, length - 10, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

PS:从上图看,浮雕效果不明显。把字体设置大点后可以看得清晰些。需要其他效果可以继承MaskFilter来自定义。

6、RasterizerSpan 光栅效果

spanText = new SpannableString("StrikethroughSpan");
spanText.setSpan(new StrikethroughSpan()07, Spannable.
SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

7、StrikethroughSpan 删除线(中划线)

spanText = new SpannableString("StrikethroughSpan");
spanText.setSpan(new StrikethroughSpan()07, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

8、SuggestionSpan

相当于占位符,一般用在EditText输入框中。当双击此文本时,会弹出提示框选择一些建议(推荐的)文字,选中的文本将替换此占位符。在输入法上用的较多。
PS:API 14新增,暂无示例。

9、UnderlineSpan 下划线

spanText = new SpannableString("UnderlineSpan");
spanText.setSpan(new UnderlineSpan()0, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

10、AbsoluteSizeSpan 绝对大小(文本字体)

spanText = new SpannableString("AbsoluteSizeSpan");
spanText.setSpan(new AbsoluteSizeSpan(20true)0, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。

DynamicDrawableSpan drawableSpan =

 new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {
    @Override
    public Drawable getDrawable() {
        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(005050);
        return d;
    }
};
DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(

DynamicDrawableSpan.ALIGN_BOTTOM) {
    @Override
    public Drawable getDrawable() {
          Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
          d.setBounds(005050);
                return d;
            }
        };
spanText.setSpan(drawableSpan, 34, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
spanText.setSpan(drawableSpan2, 78, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

12、ImageSpan 图片

spanText = new SpannableString("ImageSpan");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
</strong>d.setBounds(005050);
spanText.setSpan(new ImageSpan(d)34, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

13、RelativeSizeSpan 相对大小(文本字体)

spanText = new SpannableString("RelativeSizeSpan");
//参数proportion:比例大小
spanText.setSpan(new RelativeSizeSpan(2.5f)34,

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

15、ScaleXSpan 基于x轴缩放

spanText = new SpannableString("ScaleXSpan -- 萝卜白菜的博客");
//参数proportion:比例大小
spanText.setSpan(new ScaleXSpan(3.8f)37, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

16、StyleSpan 字体样式:粗体、斜体等

spanText = new SpannableString("StyleSpan -- 萝卜白菜的博客");
//Typeface.BOLD_ITALIC:粗体+斜体
spanText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC)37,

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

17、SubscriptSpan 下标(数学公式会用到)

spanText = new SpannableString("SubscriptSpan -- 萝卜白菜的博客");
spanText.setSpan(new SubscriptSpan()67, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

18、SuperscriptSpan 上标(数学公式会用到)

spanText = new SpannableString("SuperscriptSpan -- 萝卜白菜的博客");
spanText.setSpan(new SuperscriptSpan()67, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)

spanText = new SpannableString("TextAppearanceSpan -- 萝卜白菜的博客");
//若需自定义TextAppearance,可以在系统样式上进行修改
spanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium),

 67, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

PS:系统还提供了相关值TextAppearance_Small, TextAppearance_Large等。如有需要可在以上样式基础上修改。

20、TypefaceSpan 文本字体

spanText = new SpannableString("TypefaceSpan -- 萝卜白菜的博客");
//若需使用自定义字体,可能要重写类TypefaceSpan
spanText.setSpan(new TypefaceSpan("monospace")310,

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);

21、URLSpan 文本超链接

spanText = new SpannableString("URLSpan -- 萝卜白菜的博客");
spanText.setSpan(new URLSpan("http://orgcent.com")10, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mTVText.append("\n");
mTVText.append(spanText);
//让URLSpan可以点击
mTVText.setMovementMethod(new LinkMovementMethod());



你可能感兴趣的:(TextView使用SpannableString设置复合文本)