如何改变的EditText光标高度? [英] How to change EditText cursor height?

查看:2923
本文介绍了如何改变的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.

请注意:仅适用于12 API和更大由于对 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

我通常最终会使用负底部填充缩短光标,因为它降得过低低于基线,当你增加与 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" />

相关内部Android源$ C ​​$ C Editor.java 从中我想出解决办法:

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光标高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆