TextView多样式字体实现

先看下图

TextView多样式字体实现_第1张图片

TextView是可以修改字体大小,颜色等。要想实现自定义的文字,我们需要用到SpannableString。

 public class MainActivity extends Activity {
   TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView) findViewById(R.id.text);
    //创建SpannableString对象,内容不可修改
    SpannableString ss=new SpannableString("字体测试字体绝对大小相对大小缩放大小" +
            "前景颜色背景颜色字体样式下划线删除线上标X2下标Y3链接" +
            "字体外表图图图图");
    //使用setSpan()方法设置标记对象
    //使用removeSpan()解绑标记对象
    //public void setSpan (Object what, int start, int end, int flags)
    //绑定特定的标记对象(Object what)到起始为start,终止为end的text对象上(若已绑定标记对象则先移除)
    //设置字体TypefaceSpan()
    //设置字体大小(绝对值)AbsoluteSizeSpan()
    //设置字体大小(相对值)RelativeSizeSpan()
    //设置字体大小(相对值,默认字体宽度的倍数)ScaleXSpan()
    //设置字体的前景色ForegroundColorSpan()
    //设置字体的背景色BackgroundColorSpan()
    //设置字体的样式StyleSpan()
    //设置下划线UnderlineSpan()
    //删除线StrikethroughSpan()
    //上标SuperscriptSpan()
    //下标SubscriptSpan()
    //链接URLSpan()-->需要添加setMovementMethod方法附加响应
    //字体外观设置(依次包括字体名称,字体大小,字体样式,字体颜色,链接颜色)TextAppearanceSpan()
    //项目符号BulletSpan()
    //图片ImageSpan()

    //设置字体(default,default-bold,monospace,serif,sans-serif)
    ss.setSpan(new TypefaceSpan("sans-serif"),0,4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new AbsoluteSizeSpan(30,true),4,10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new RelativeSizeSpan(0.5f),10,14,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    ss.setSpan(new ScaleXSpan(1.5f),14,18,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new ForegroundColorSpan(Color.BLUE),18,22,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new BackgroundColorSpan(Color.YELLOW),22,26,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new StyleSpan(Typeface.BOLD),26,30,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new UnderlineSpan(),30,33,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new StrikethroughSpan(),33,36,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new SuperscriptSpan(),39,40,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new SubscriptSpan(),43,44,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new URLSpan("http://www.baidu.com"),44,46,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


    ColorStateList color = null;
    ColorStateList linkColor = null;

    XmlResourceParser parser = getResources().getXml(R.drawable.colorselector);
    try {
        color = ColorStateList.createFromXml(getResources(),parser);
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        linkColor = ColorStateList.createFromXml(getResources(),parser);
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ss.setSpan(new TextAppearanceSpan("serif",Typeface.BOLD_ITALIC,40,color,linkColor),46,50,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
    drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
    ss.setSpan(new ImageSpan(drawable), 50, 54, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(new BulletSpan(BulletSpan.STANDARD_GAP_WIDTH,Color.RED),0,ss.length(),Spanned.SPAN_COMPOSING);
    mTextView.setMovementMethod(LinkMovementMethod.getInstance());
    mTextView.setText(ss);
 }
}

**
转载自: http://www.cnblogs.com/JohnTsai/p/4547716.html
这篇写的也很好,也给大家推荐下:
http://www.jianshu.com/p/509b0d2626f4
http://www.jianshu.com/p/84067ad289d2#
**

你可能感兴趣的:(工作笔记)