How to set EditText custom cursor-drawable?

孤者浪人 提交于 2021-01-28 08:17:44

问题


I have the following EditText

<EditText
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        android:layout_gravity="end"
        android:textCursorDrawable="@drawable/hammer_cursor"
        android:layout_margin="10dp"
        android:textIsSelectable="false"
        android:longClickable="false"
        android:textColor="#858383"
        android:background="@drawable/backer"
        android:textSize="14sp"
        android:id="@+id/name"
        android:padding="10dp"
        android:hint="message"
        android:textColorHint="#CDCDCD"
        tools:ignore="SmallSp"
        />

in a framelayout. I am trying to make the cursor a hammer but despite having the hammer-drawable available in hdpi,mdpi,xhdpi or even making the hammer-image from android:textCursorDrawable="@drawable/hammer_cursor"smaller the drawable fails to scale correctly to fit the cursor(even when it scales correctly with a smaller hammer-image, it's still blurry). Is there a standard dimension for the EditText-cursor-drawable in android(so I make the hammer-image accordingly) or how can the drawable be set correctly to fit the cursor without any stretching or blurriness of the hammer-image.


回答1:


Remove android:hint="Message" this line from your code. Use vector image instead of any-other image type. Make new file in your drawable forlder named hammer_cursor.xml and put this code inside this file.

Here is some example of hammer image:

<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="vector"
    android:width="31dp"
    android:height="31dp"
    android:viewportWidth="512"
    android:viewportHeight="512">
<path
        android:name="path"
        android:pathData="M 486.391 195.313 C 480.527 189.449 471.039 189.449 465.176 195.313 L 454.57 205.918 L 433.355 184.703 L 369.719 248.344 L 390.93 269.559 L 380.324 280.164 C 374.461 286.027 374.461 295.516 380.324 301.379 L 401.539 322.59 C 407.398 328.453 416.891 328.453 422.754 322.59 L 507.605 237.738 C 513.465 231.875 513.465 222.387 507.605 216.523 Z M 486.391 195.313 M 337.898 237.738 L 422.75 152.883 C 428.613 147.023 428.613 137.535 422.75 131.672 L 337.898 46.82 C 307.703 16.625 267.566 0 224.871 0 L 157.586 0 C 150.977 0 145.145 4.32 143.23 10.648 C 141.313 16.977 143.715 23.938 149.27 27.48 L 226.633 79.055 C 235.137 84.805 240.668 93.918 241.68 104.129 C 242.695 114.363 239.102 124.402 231.832 131.672 C 225.969 137.535 225.969 147.023 231.832 152.887 L 316.684 237.738 C 322.547 243.602 332.035 243.602 337.898 237.738 Z M 337.898 237.738 M 242.438 354.41 C 248.301 348.547 248.301 339.059 242.438 333.199 L 178.797 269.559 C 172.938 263.695 163.449 263.695 157.586 269.559 L 125.766 301.379 L 210.617 386.23 Z M 242.438 354.41 M 17.551 409.445 C -5.852 432.844 -5.852 470.898 17.551 494.297 C 40.949 517.695 79.004 517.695 102.402 494.297 L 189.406 407.445 L 104.551 322.59 Z M 17.551 409.445 M 200.008 248.344 L 242.434 205.918 L 306.074 269.559 L 263.648 311.984 Z M 200.008 248.344"
        android:fillColor="#000"
        android:strokeWidth="1"/>



来源:https://stackoverflow.com/questions/55995452/how-to-set-edittext-custom-cursor-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!