Android设置textview的字体之间的间距

 看到这个问题,我查看了下网上有很多说设置

textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型。查看源代码,默认textView 此属性是使用的是:

android.internal.R.styleable.TextView_textScaleX

setTextScaleX(a.getFloat(attr, 1.0f));

但是这个显示的效果只是拉伸,并没有改变字体间的间距

根据Android api解释:


android:textScaleX     setTextScaleX(float)    Sets the horizontal scaling factor for the text. 

只是设置了文本的水平缩放,

android:letterSpacing  setLetterSpacing(float)   Text letter-spacing. 

这个是设置文本缩放,但是只有在API>=21,才能使用


所以重写textview;


public class LetterSpacingTextView extends TextView {

    private float spacing = Spacing.NORMAL;
    private CharSequence originalText = "";


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

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

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

    public float getSpacing() {
        return this.spacing;
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LetterSpacingTextView);
        originalText = array.getString(R.styleable.LetterSpacingTextView_text);
        setSpacing(array.getFloat(R.styleable.LetterSpacingTextView_textSpacing, 0));
        array.recycle();
    }

    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()) {
                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);
    }

    public class Spacing {
        public final static float NORMAL = 0;
    }
}


attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="LetterSpacingTextView">
        <attr name="textSpacing" format="float"/>
        <attr name="text" format="string"/>
    </declare-styleable>
</resources>
xml中使用示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.akm.akmviewpager.LetterSpacingTextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:text="暗淡轻黄体性柔,情疏迹远只香留。 何须浅碧轻红色,自是花中第一流。梅定妒,菊应羞,画栏开处冠中秋。 骚人可煞无情思,何事当年不见收。"
        app:textSpacing="5" />
</LinearLayout>

Java代码中使用示例:

 LetterSpacingTextView textView = (LetterSpacingTextView) findViewById(R.id.textView);
        textView.setText("出自宋代诗人李清照的《鹧鸪天·桂花》");
        textView.setSpacing(5);</span>
改编自:How to adjust text kerning in Android TextView? 

查看:http://blog.csdn.net/aikongmeng/article/details/50553806
增加了xml配置字间距的属性




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值