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

来源:互联网 发布:苹果版手机淘宝 编辑:程序博客网 时间:2024/05/01 18:41

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

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配置字间距的属性




0 0