How to change EditText cursor height?

后端 未结 3 765
独厮守ぢ
独厮守ぢ 2021-01-02 02:10

I want to change the EditText cursor height, does anyone know how?

相关标签:
3条回答
  • 2021-01-02 02:13

    Paddings can be used to control cursor height

    cursor.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <size android:width="2dp" />
        <solid android:color="@color/black" />
    </shape>
    

    cursor_with_padding.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="7dp"
            android:drawable="@drawable/cursor"
            android:top="8dp" />
    </layer-list>
    

    layout

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textCursorDrawable="@drawable/cursor_with_padding"/>
    
    0 讨论(0)
  • 2021-01-02 02:21

    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.

    note: only works on API 12 and greater due to support for 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

    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.

    example 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>
    

    This will make a 2dip wide red cursor that is

    • 2sp higher (longer) at the top
    • 11sp higher (shorter) on the bottom.

    Then in your edittext, just specify android:textCursorDrawable:

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

    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);
    }
    
    0 讨论(0)
  • 2021-01-02 02:28

    change the editText style to a custom style

    <item name="android:editTextStyle">@style/myEditText</item>
    

    then use a drawable to set the cursor:

    <style name="myEditText" parent="@android:parentdetails">
    <item name="android:textCursorDrawable">@android:drawable/my_cursor_drawable</item>
    <item name="android:height">Height here e.g. 50sp</item> 
    </style>
    
    0 讨论(0)
提交回复
热议问题