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;
    }
}
相關文章
相關標籤/搜索