欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TextView多样式字体实现

程序员文章站 2022-06-09 20:19:02
...

先看下图

TextView多样式字体实现

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);
 }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

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

转自:https://blog.csdn.net/qq_32234421/article/details/53005742