android - 如何在 Android TextView 中调整文本字距?

标签 android textview spacing kerning

有没有办法调整 Android TextView 中字符之间的间距?我相信这通常被称为“字距调整”。

我知道 android:textScaleX 属性,但它会压缩字符和间距。

最佳答案

我构建了一个自定义类,它扩展了 TextView 并添加了一个方法“setSpacing”。解决方法类似于@Noah 所说的。该方法在字符串的每个字母和 SpannedString 之间添加一个空格。更改空格的 TextScaleX,允许正负间距。

希望对某人有所帮助^^

/**
 * Text view that allows changing the letter spacing of the text.
 * 
 * @author Pedro Barros (pedrobarros.dev at gmail.com)
 * @since May 7, 2013
 */

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

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

    public float getSpacing() {
        return this.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()) {
                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;
    }
}

使用它:

LetterSpacingTextView textView = new LetterSpacingTextView(context);
textView.setSpacing(10); //Or any float. To reset to normal, use 0 or LetterSpacingTextView.Spacing.NORMAL
textView.setText("My text");
//Add the textView in a layout, for instance:
((LinearLayout) findViewById(R.id.myLinearLayout)).addView(textView);

关于android - 如何在 Android TextView 中调整文本字距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1640659/

相关文章:

java - 有谁知道如何在 Eclipse 中为 java 分隔控制台文本

android - 在现有驱动器文件夹中创建文件/文件夹

android - 在 Activity 和 MapActivity 之间共享一个选项菜单

Android TextView 超链接

PHP 包含 SQL 结果(分页)的 float div 和另一个包含内容的 div 之间的间距

html - 使用 Bootstrap 响应方案的框之间的间距

android - 在抽屉导航中添加 2 个 ListView ,只有一个有效

android - MPAndroidChart 饼图如何不显示底部标签?

安卓 TextView : setting the background color dynamically doesn't work

android - 将两个 TextView 并排放置在布局中