EditText上的字母间距不起作用

3

我希望在EditText上设置字母间距,但是没有效果。

我尝试像这样创建EditText:

 <EditText
                android:id="@+id/pin"
                android:layout_width="match_parent"
                android:layout_height="@dimen/input_text_height"
                android:textSize="@dimen/text_size_extra_large"
                local:MvxBind="Text PinCode"
                android:maxLength="6"
                android:layout_marginRight="32dp"
                android:layout_marginLeft="32dp"
                android:layout_marginTop="10dp"
                android:padding="6dp"
                android:gravity="center"
                android:textColor="@drawable/editText_textColorState"
                android:background="@drawable/editText_borderOnFocus"
                android:letterSpacing="10"/>

字母间距属性无法生效。我做错了什么?

安卓API版本是多少? - sasikumar
Android 5.0 - API 21 - Kam
尝试在物理设备上运行应用程序。 - Rahul Khurana
请参考以下链接:https://dev59.com/l2435IYBdhLWcg3w7ksK,了解如何在TextView中更改字母间距。 - sasikumar
这就是我正在做的。我没有在模拟器上运行应用程序。 - Kam
显示剩余3条评论
3个回答

12

letterSpacing 需要是浮点数。尝试使用 android:letterSpacing="0.50"

像 @mike 指出的那样,自 Android Studio V4.0 以来可以在预览中呈现字距。

旧答案

Android Studio 在预览中不会呈现字距,因此您必须手动在设备上运行以查看更改。


1
给未来的读者一个小提示:在Android Studio 4.0.1及以上版本中,字母间距会实时更新在预览中。 - Michele

2

在官方文档中,他们提到需要使用浮点数值而不是整数。

android:letterSpacing 文本字母间距。

可以是浮点数值,例如"1.2"。

最初的回答中提到,android:letterSpacing属性需要用浮点数值而不是整数。请注意,这个属性控制文本字母之间的间距。例如,如果你想让字母之间的间距增加1.2倍,你应该将android:letterSpacing设置为"1.2"。

android:letterSpacing="1.2"

"最初的回答"应该是这样的:
因此,您的EditText应该长这样
<EditText
    android:id="@+id/pin"
    android:layout_width="match_parent"
    android:layout_height="@dimen/input_text_height"
    android:textSize="@dimen/text_size_extra_large"
    local:MvxBind="Text PinCode"
    android:maxLength="6"
    android:layout_marginRight="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="10dp"
    android:padding="6dp"
    android:gravity="center"
    android:textColor="@drawable/editText_textColorState"
    android:background="@drawable/editText_borderOnFocus"
    android:letterSpacing="10.0"/>

0

你需要省略单位,只输入数值:

android:letterSpacing="1.2"

由于某些原因,AS预览中的字母间距未发生变化。我必须在实际设备上运行应用程序才能看到更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接