Java设置edittext光标,如何改变的EditText光标高度?
I want to change the EditText cursor height, does anyone know how?
解決方案
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.
note: only works on API 12 and greater due to support for textCursorDrawable
Use positive top padding to move the top of your cursor higher
User positive bottom padding to move the bottom of your cursor lower
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.
example cursor_red.xml:
android:shape="rectangle" >
android:width="2dip" />
android:color="@color/red" />
android:top="2sp"
android:bottom="-11sp" />
This will make a 2dip wide red cursor that is
2sp higher (longer) at the top
11sp higher (shorter) on the bottom.
Then in your edittext, just specify android:textCursorDrawable:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/cursor_red" />
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);
}
總結(jié)
以上是生活随笔為你收集整理的Java设置edittext光标,如何改变的EditText光标高度?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql 调用未定义函数_php –
- 下一篇: java里面什么时候环境变量_Java的