android - 如何设置 EditText 自定义光标可绘制?

标签 android android-edittext

我有以下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"
        />

在框架布局中。我试图使光标成为锤子,但尽管在 hdpimdpixhdpi 中提供了 hammer-drawable,甚至制作了锤子-来自 android:textCursorDrawable="@drawable/hammer_cursor" 的图像越小,drawable 就无法正确缩放以适合光标(即使它使用较小的锤子图像正确缩放,它仍然很模糊)。 Android 中的 EditText-cursor-drawable 是否有标准尺寸(因此我相应地制作了锤子图像)或者如何正确设置可绘制对象以适合光标而不会出现任何拉伸(stretch)或模糊锤子图像。

最佳答案

从您的代码中删除 android:hint="Message" 这一行。 使用矢量图像而不是任何其他图像类型。 在名为 hammer_cursor.xml 的 drawable forlder 中创建新文件,并将此代码放入此文件中。

这里是一些锤子图像的例子:

<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"/>

enter image description here

关于android - 如何设置 EditText 自定义光标可绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55995452/

相关文章:

java - 我对 abiFilters 配置有疑问

javascript - 消除 Android 网络应用程序的 onclick 延迟

css - Android 下拉(选择)CSS

android - 在运行时构建布局 android

android - 如何在 MainActivity 上显示使用 EditText 在自定义 alertDialogFragment 上插入的数字?

Android EditText 事件监听器

java - JNI方法静态解析验证

Android 在 EditText 上方显示 EditText.setError() 而不是在其下方

android - ScrollView 内的可滚动 EditText

android - 添加的 EditText 不是 TextInputEditText。请改用该类(class)