android - 如何更改 EditText 光标高度?

标签 android android-edittext

我想更改 EditText 光标高度,有人知道怎么做吗?

最佳答案

我必须深入研究 Android 源代码才能找到这个问题的答案,但您基本上必须在自定义形状可绘制对象上使用填充。

注意:由于支持 textCursorDrawable仅适用于 API 12 及更高版本

使用positive top padding 将光标的top 移到

用户底部填充将光标的底部移动到

我通常最终会使用负底部填充来缩短光标,因为当您使用 lineSpacingMultiplier 增加行高时,它会下降到基线以下太低或 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 宽的红色光标

  • 顶部高出(更长)2sp
  • 底部高(短)11sp。

然后在您的编辑文本中,只需指定 android:textCursorDrawable :

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textCursorDrawable="@drawable/cursor_red" />

Editor.java 中的相关 Android 源代码,我从中找到了解决方案:

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);
}

关于android - 如何更改 EditText 光标高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641997/

相关文章:

java - 从 Firebase 检索值并显示在 EditText 中

listview中的Android edittext失去了对调用notifydatachanged的关注

android - TextView 中的行号、代码高亮显示

android - 识别编辑文本中的当前主题标签或标签

java - 如何在 Eclipse 中导入 ShowcaseView?

java - 有没有办法在 Android 上对 java 进程的部分进行沙箱处理?

android - 为从移动应用程序访问 AWS S3 生成预​​签名 URL

java - Android Java语音识别只需要1个结果......如何获得1个结果?

java - 如何从 Android 中的应用程序中注销用户

Android EditText Style.xml 改变FOCUS EditText