Android字间距和行间距

针对行间距能够直接在xml中写 一、android:lineSpacingExtra 设置行间距 二、android:lineSpacingMultiplier 设置行间距的倍数android

修改字间距可使用如下代码app

package com.okkuaixiu.combo.view;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ScaleXSpan;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 能够设置字体间距的类
 */
public class AlignSpacingTextView extends TextView {

    private float spacing = Spacing.NORMAL;//字体间距
    private CharSequence originalText = "";

    public AlignSpacingTextView(Context context) {
        super(context);
    }

    public AlignSpacingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AlignSpacingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 获取字间距
     *
     * @return
     */
    public float getSpacing() {
        return this.spacing;
    }

    /**
     * 设置间距
     *
     * @param spacing
     */
    public void setSpacing(float spacing) {
        this.spacing = spacing;
        applySpacing();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        applySpacing();
    }

    @Override
    public CharSequence getText() {
        return originalText;
    }

    /**
     * 添加应用空间
     */
    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()) {
                // \u00A0 不间断空格
                // 追加空格
                builder.append("\u00A0");
            }
        }
        // TextView一般用来显示普通文本,可是有时候须要对其中某些文本进行样式、事件方面的设置。Android系统经过SpannableString类来对指定文本进行相关处理,具体有如下功能:
        // 一、BackgroundColorSpan 背景色
        // 二、ClickableSpan 文本可点击,有点击事件
        // 三、ForegroundColorSpan 文本颜色(前景色)
        // 四、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
        // 五、MetricAffectingSpan 父类,通常不用
        // 六、RasterizerSpan 光栅效果
        // 七、StrikethroughSpan 删除线(中划线)
        // 八、SuggestionSpan 至关于占位符
        // 九、UnderlineSpan 下划线
        // 十、AbsoluteSizeSpan 绝对大小(文本字体)
        // 十一、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。
        // 十二、ImageSpan 图片
        // 1三、RelativeSizeSpan 相对大小(文本字体)
        // 1四、ReplacementSpan 父类,通常不用
        // 1五、ScaleXSpan 基于x轴缩放
        // 1六、StyleSpan 字体样式:粗体、斜体等
        // 1七、SubscriptSpan 下标(数学公式会用到)
        // 1八、SuperscriptSpan 上标(数学公式会用到)
        // 1九、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
        // 20、TypefaceSpan 文本字体
        // 2一、URLSpan 文本超连接
        // 咱们也是经过这个,去设置空格
        SpannableString finalText = new SpannableString(builder.toString());
        if (builder.toString().length() > 1) { // 若是当前TextView内容长度大于1,则进行空格添加
            for (int i = 1; i < builder.toString().length(); i += 2) { // 小demo:100  1 0 0
                // 按照x轴等比例进行缩放 经过咱们设置的字间距+1除以10进行等比缩放
                finalText.setSpan(new ScaleXSpan((spacing + 1) / 10), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        super.setText(finalText, TextView.BufferType.SPANNABLE);
    }

    public class Spacing {
        public final static float NORMAL = 0;
    }
}
相关文章
相关标签/搜索