我想改变 EditText
光标高度,有人知道怎么做吗?
I want to change the EditText
cursor height, does anyone know how?
我不得不深入研究 Android 源代码才能找到答案,但实际上您必须在自定义形状可绘制对象上使用填充.
I had to dig far into the Android source to find the answer to this, but you essentially have to use padding on a custom shape drawable.
注意:仅适用于 API 12 及更高版本,因为支持 textCursorDrawable
使用 positive top 内边距将光标的 top 移动更高
Use positive top padding to move the top of your cursor higher
用户正 bottom填充移动光标的底部降低
我通常最终使用负底部填充来缩短光标,因为当您使用 lineSpacingMultiplier
或 lineSpacingExtra
增加行高时,光标会下降到低于基线的位置.
I usually end up using negative bottom padding to shorten the cursor because the it drops too low below baseline when you increase the line height with lineSpacingMultiplier
or 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 宽的红色光标
This will make a 2dip wide red cursor that is
然后在你的edittext中,指定android:textCursorDrawable
:
Then in your edittext, just specify android:textCursorDrawable
:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/cursor_red" />
Editor.java中的相关Android源代码,我从中找到了解决方案:
Relevant Android source code inside Editor.java from which I figured out the solution:
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);
}
这篇关于如何更改 EditText 光标高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!