Android SpannableString 的用法

来源:互联网 发布:mac os x86 编辑:程序博客网 时间:2024/05/21 07:07

      当屏幕上一行既显示文字又显示图片时候,如果文字过长,图片可能被截断。假如文字的长度是不固定的,又不希望图片被截断,也不希望总是分成两行显示, 可以用SpannableString:文字短时,文字图片都在一行显示;当文字过长时,图片会自动换行而不会被截断。

package com.test.mytest;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Typeface;import android.text.SpannableStringBuilder;import android.text.Spanned;import android.text.method.LinkMovementMethod;import android.text.style.BackgroundColorSpan;import android.text.style.ForegroundColorSpan;import android.text.style.ImageSpan;import android.text.style.RelativeSizeSpan;import android.text.style.StyleSpan;import android.text.style.URLSpan;import android.text.style.UnderlineSpan;import android.widget.TextView;public class MyTestClass1 {    public static void setSpanForHyperText(Context context, TextView textView, String hyperText,             int colorValue, String url, Bitmap bitmap) {        SpannableStringBuilder spanned = new SpannableStringBuilder(hyperText);        int start = 0;        int end = spanned.length();        // font size (relative SMALL)        spanned.setSpan(new RelativeSizeSpan(0.75f), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        // font size (relative LARGE)        spanned.setSpan(new RelativeSizeSpan(1.25f), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        // font style (bold and underline)        spanned.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        spanned.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        // text background color        spanned.setSpan(new BackgroundColorSpan(colorValue), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        // text foreground color        spanned.setSpan(new ForegroundColorSpan(colorValue), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        // text link        if (null != url) {            spanned.setSpan(new URLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        }                // Image Span        // "a" just stands for a place-holder, no other meaning.         // after calling setSpan() the span object will replace the place-holder.        spanned.append("a");        end = spanned.length();        ImageSpan imageSpan = new ImageSpan(context, bitmap, ImageSpan.ALIGN_BASELINE);        spanned.setSpan(imageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);                textView.setText(spanned);        // Links require the movement method to be set properly to work        if (!(textView.getMovementMethod() instanceof LinkMovementMethod)) {            textView.setMovementMethod(LinkMovementMethod.getInstance());        }    }}

//文本可点击,有点击事件spanned.setSpan(new ClickableSpan() {    @Override     public void onClick(View widget) {         //TODO     }     @Override     public void updateDrawState(TextPaint ds) {         ds.setColor(ds.linkColor);         ds.setUnderlineText(false); //去掉下划线     }}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    1、BackgroundColorSpan 背景色 
    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 文本超链接

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

0 0