Android TextView中字体行间距的设置

Android系统默认不提供TextView字间距设置方法,但可以通过继承TextView并重写setText()和getText(),添加setLetterSpacing()方法来实现。具体实现方式包括在自定义的LetterSpacingTextView中设置间距属性,并在attrs.xml中声明属性,从而允许在XML布局文件中直接设置。此外,代码中展示了如何在运行时动态调整间距。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       通过查看Android TextView属性设置方法,发现当前版本的 Android 似乎并未提供控制 TextView 的字间距 方法。
百度搜索网上发现大量“教程”声称可以利用 TextView 的  setTextScaleX() 方法设置字间距,但从字面上(Scale)就可看出其实它是用于设置字体的缩放比率(试验结果亦是如此),真的是误导别人。
最后从国外的一家论坛上(StackOverFlow)发现一个解决方案:通过继承 TextView 并重写  setText() 和  getText()   方法,增加  setLetterSpacing() 等方法实现该需求。而TextView本身的属性没有影响.
先看效果图吧

代码整理如下:
LetterSpacingTextView.java
package com.example.textviewspace;

/**
 * cmos
 * 字间距
 */

import android.content.Context;
import android.content.res.TypedArray;
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);
		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;
	}
}
由于我们设置的时候,有时想通过xml文件中就设置好space,所以我们就自定义一下我们LetterSpacingTextView的attrs文件,给其设置属性,这样我们就可以在xml中设置好间距space了。
attrs.xml (values.xml文件夹下新建一个这样的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>
主布局文件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:cmos="http://schemas.android.com/apk/res/com.example.textviewspace"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.textviewspace.MainActivity" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通过继承 TextView并重写 setText()和 getText()方法,增加 setLetterSpacing()等方法实现该需求。" />

    <com.example.textviewspace.LetterSpacingTextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv1"
        android:textColor="#678933"
        cmos:textSpacing="5"
        android:textSize="24sp" />

</RelativeLayout>

注意上述代码的第三行,由于我们用到了自定义的attrs文件,想要设置view的属性,就必须得先声明命名空间(xmlns),当然名字你可以随便取。
设置好后,我们可以在自定义的View中设置属性了,如第20行。
当然,你也可以在代码中设置间距
tv2=(LetterSpacingTextView) findViewById(R.id.tv2);
		tv2.setText("通过继承 TextView并重写setText()和 getText()方法,增加 setLetterSpacing()等方法实现该需求。");
		tv2.setSpacing(8);


当然,如你所见,这样做还有个缺陷,不是很完美,比如,文字在换行时,显得有点突兀,当然,这不是这个方法的缺陷,而是TextView文本在显示方面没做好,后期有时间再去找一下解决方案。

至此,本文已结束,如果对文章有什么疑惑,欢迎留言!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值