android:lineSpacingMultiplier="1.2" -- 设定行高为标准行高的1.2倍
android:lineSpacingExtra="20dp"
-- 设定行高在标准行高的基础上再增加 20 dp
<EditText
android:id="@+id/cake"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/toolbar"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:background="@android:color/holo_orange_light"
android:textColor="@android:color/white"
android:textCursorDrawable="@drawable/cursor"
android:hint="Type here..." />
当设定的行高大于标准行高之后
会出现光标的高度要比文字高度高的问题
如图所示:
这个问题的解决方法是:
在自定义的光标样式文件中对光标样式加以特殊处理
android:textCursorDrawable="@drawable/cursor"
cursor.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size android:width="1dp" />
<solid android:color="@android:color/white" />
<padding
android:top="1sp"
android:bottom="-3sp" />
</shape>
top = 1sp 这个会让光标向上多延伸 1 sp 的高度
bottom = -3sp 这个会让光标在下面缩短 3sp 的高度
这两个值不是固定的。
需要根据你设定的行间距来做调整。
调整后的效果如下:(1.2倍的行高,设定 cursor 的top = 0sp, bottom = -3sp)
这个应该解决了
请教 android EditText设置行间距后字符怎么不居中显示了 这个问题。