我想更改 EditText
光标高度,有人知道怎么做吗?
最佳答案
我必须深入研究 Android 源代码才能找到这个问题的答案,但您基本上必须在自定义形状可绘制对象上使用填充。
注意:由于支持 textCursorDrawable
,仅适用于 API 12 及更高版本
使用positive top padding 将光标的top 移到
用户正底部填充将光标的底部移动到下
我通常最终会使用负底部填充来缩短光标,因为当您使用 lineSpacingMultiplier
增加行高时,它会下降到基线以下太低或 lineSpacingExtra
.
cursor_red.xml 示例:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size
android:width="2dip" />
<solid
android:color="@color/red" />
<padding
android:top="2sp"
android:bottom="-11sp" />
</shape>
这将生成一个 2dip 宽的红色光标
- 顶部高出(更长)2sp
- 底部高(短)11sp。
然后在您的编辑文本中,只需指定 android:textCursorDrawable
:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/cursor_red" />
Editor.java 中的相关 Android 源代码,我从中找到了解决方案:
private void updateCursorPosition(int cursorIndex, int top, int bottom, float horizontal) {
...
mCursorDrawable[cursorIndex].getPadding(mTempRect);
...
mCursorDrawable[cursorIndex].setBounds(left, top - mTempRect.top, left + width,
bottom + mTempRect.bottom);
}
关于android - 如何更改 EditText 光标高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641997/