How to change EditText cursor height?(如何更改 EditText 光标高度?)
问题描述
我想改变 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
- 顶部高出 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中的相关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 光标高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改 EditText 光标高度?


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01