問題描述
我想改變 EditText
光標(biāo)高度,有人知道怎么做嗎?
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 內(nèi)邊距將光標(biāo)的 top 移動更高
Use positive top padding to move the top of your cursor higher
用戶正 bottom填充移動光標(biāo)的底部降低
我通常最終使用負(fù)底部填充來縮短光標(biāo),因為當(dāng)您使用 lineSpacingMultiplier
或 lineSpacingExtra
增加行高時,光標(biāo)會下降到低于基線的位置.
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 寬的紅色光標(biāo)
This will make a 2dip wide red cursor that is
- 頂部高出 2sp(更長)
- 底部高出(短)11sp.
然后在你的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中的相關(guān)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);
}
這篇關(guān)于如何更改 EditText 光標(biāo)高度?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!