【发布时间】:2011-07-05 05:58:23
【问题描述】:
如何更改文本视图中的字母间距? 如果我有 HTML 文本会有帮助吗(我不能在我的代码中使用 webview)。
附:我在带有 HTML 文本的 textview 中使用我自己的字体。
【问题讨论】:
-
在布局编辑器中你可以做
android:letterSpacing=".05"在像 photoshop 这样的程序中,0.05 大约是“50”
如何更改文本视图中的字母间距? 如果我有 HTML 文本会有帮助吗(我不能在我的代码中使用 webview)。
附:我在带有 HTML 文本的 textview 中使用我自己的字体。
【问题讨论】:
android:letterSpacing=".05" 在像 photoshop 这样的程序中,0.05 大约是“50”
从 API 21 开始,有一个选项集字母间距。您可以调用方法setLetterSpacing 或使用属性letterSpacing 在XML 中设置它。
【讨论】:
letterSpacing 在 AS 预览中没有改变。我必须在物理设备上实际运行应用程序才能看到变化。
更多空间:
android:letterSpacing="0.1"
空间更小:
android:letterSpacing="-0.07"
【讨论】:
letterSpacing为0.0
根据您需要多少间距,这可能会有所帮助。这是唯一与 TextView 中的字母间距相关的东西。
编辑: 请参阅下面的@JerabekJakub's response,了解从 api 21 (Lollipop) 开始的更新、更好的方法
【讨论】:
letterSpacing 与 textScaleX 差别太大了
此答案基于佩德罗的答案,但经过调整,因此如果已设置 text 属性,它也可以工作:
package nl.raakict.android.spc.widget;
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 letterSpacing = LetterSpacing.BIGGEST;
private CharSequence originalText = "";
public LetterSpacingTextView(Context context) {
super(context);
}
public LetterSpacingTextView(Context context, AttributeSet attrs){
super(context, attrs);
originalText = super.getText();
applyLetterSpacing();
this.invalidate();
}
public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
public float getLetterSpacing() {
return letterSpacing;
}
public void setLetterSpacing(float letterSpacing) {
this.letterSpacing = letterSpacing;
applyLetterSpacing();
}
@Override
public void setText(CharSequence text, BufferType type) {
originalText = text;
applyLetterSpacing();
}
@Override
public CharSequence getText() {
return originalText;
}
private void applyLetterSpacing() {
if (this == null || this.originalText == null) return;
StringBuilder builder = new StringBuilder();
for(int i = 0; i < originalText.length(); i++) {
String c = ""+ originalText.charAt(i);
builder.append(c.toLowerCase());
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((letterSpacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
super.setText(finalText, BufferType.SPANNABLE);
}
public class LetterSpacing {
public final static float NORMAL = 0;
public final static float NORMALBIG = (float)0.025;
public final static float BIG = (float)0.05;
public final static float BIGGEST = (float)0.2;
}
}
如果您想以编程方式使用它:
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);
【讨论】:
null 签入applyLetterSpacing,但除此之外,还有救命稻草!
在 API >=21 之后有一个由 TextView 提供的 inbuild 方法,称为 setLetterSpacing
check this了解更多
【讨论】:
我构建了一个扩展TextView 的自定义类并解决了这个问题...查看我的答案here =)
【讨论】:
由于android不支持这样的事情,你可以用FontCreator手动完成。它有很好的字体修改选项。 我使用这个工具来构建一个自定义字体,即使它需要一些时间,但你总是可以在你的项目中使用它。
【讨论】:
要在文本视图中嵌入 HTML 文本,您可以使用 Html.fromHTML() 语法。
更多信息,您将从http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29获得更多信息
【讨论】: