美文网首页
TextView 行间距以及字间距

TextView 行间距以及字间距

作者: young_xst | 来源:发表于2017-05-05 02:55 被阅读0次

    转载自:Textview

    SpannableString使用详解

    用SpannableString打造绚丽多彩的文本显示效果

    行间距

    行与行之间的间距,xml设置lineSpacingExtra属性

    lineSpacingMultiplier属性,行与行间距的倍数

    android:lineSpacingExtra="3dp"

    android:lineSpacingMultiplier="1.5"

    字间距

    自定义textview

    1.继承自Textview,三个构造方法,重写gettext和settext 方法,并且通过这两个方法得到textview的内容以及设置内容

    2.通过getSpacing 和 setSpacing 方法得到和设置字体间间距

    3.applySpacing

    public class RankTextView extends TextView {

    private float spacing = Spacing.NORMAL;

    private CharSequence originalText;

    public RankTextView(Context context) {

    super(context);

    }

    public RankTextView(Context context, AttributeSet attrs) {

    super(context, attrs);

    }

    public RankTextView(Context context, AttributeSet attrs, int defStyleAttr) {

    super(context, attrs, defStyleAttr);

    }

    public float getSpacing() {

    return this.spacing;

    }

    public void setSpacing(float spacing) {

    this.spacing = spacing;

    applySpacing();

    }

    private void applySpacing () {

    if (this == null || this.originalText ==  null) {

    return;

    }

    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < originalText.length(); i++) {

    builder.append(originalText.charAt(i));

    if (i + 1 < originalText.length()) {

    builder.append("\u00A0");

    }

    }

    SpannableString finalText = new SpannableString(builder.toString());

    if (builder.toString().length() > 1) {

    for (int i = 1; i < builder.toString().length(); i += 2) {

    finalText.setSpan(new ScaleXSpan((spacing + 1) /10),i,i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    } } super.setText(finalText,BufferType.SPANNABLE); } @Override public void setText(CharSequence text, BufferType type) { this.originalText = text; applySpacing(); } @Override
    public CharSequence getText() { return originalText; } public class Spacing { public final static float NORMAL = 0; } }                                                                                                                                                                           

    相关文章

      网友评论

          本文标题:TextView 行间距以及字间距

          本文链接:https://www.haomeiwen.com/subject/pbkjtxtx.html