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 光标高度?
基础教程推荐
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
