You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 10及以上自定义textCursorDrawable不生效问题求助

问题:Android 10+ 自定义光标样式不生效

XML 代码

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/email_et_ll"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:boxStrokeColor="@color/input_text_custom_outline_color"
        app:boxStrokeErrorColor="@android:color/holo_red_light"
        app:hintTextColor="@color/yellow"
        android:layout_marginStart="16sp"
        android:layout_marginTop="16sp"
        android:layout_marginEnd="16sp"
        android:hint="@string/hello_gmail_com"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/login_info_txt"
        app:shapeAppearance="@style/input_text_rounded_corner"
        app:startIconDrawable="@drawable/baseline_email_24">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/email_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cursorVisible="true"
            android:textCursorDrawable="@drawable/cursor_bg"
            />

    </com.google.android.material.textfield.TextInputLayout>

我为输入文本框设置了自定义textCursorDrawable,但该设置仅在Android 8、9设备上生效,在Android 10及更高版本(已测试11、12)中无效果,请问如何解决该问题?

截图对比

  • Android 8:
    Android 8 光标效果
  • Android 9:
    Android 9 光标效果
  • Android 10:
    Android 10 光标效果

解决方法

方法1:通过Material组件样式覆盖

Android 10起,Material组件的TextInputEditText会优先读取主题/样式中的光标配置,而非直接识别XML属性。可以在styles.xml中定义专属样式:

<style name="CustomCursorEditTextStyle" parent="Widget.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="textCursorDrawable">@drawable/cursor_bg</item>
    <!-- 可选:同时设置光标激活态颜色,确保样式统一 -->
    <item name="colorControlActivated">@color/your_cursor_color</item>
</style>

然后给TextInputEditText应用该样式:

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/email_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:cursorVisible="true"
    style="@style/CustomCursorEditTextStyle"
    />

方法2:代码动态设置

若XML配置仍不生效,可在代码中强制设置光标Drawable:

// Kotlin示例
val cursorDrawable = ContextCompat.getDrawable(this, R.drawable.cursor_bg)
email_et.setTextCursorDrawable(cursorDrawable)
// Java示例
Drawable cursorDrawable = ContextCompat.getDrawable(this, R.drawable.cursor_bg);
email_et.setTextCursorDrawable(cursorDrawable);

方法3:检查自定义Drawable格式

确保cursor_bg.xml符合Android 10+规范,示例写法如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="2dp" />
    <solid android:color="@color/yellow" />
</shape>

内容的提问来源于stack exchange,提问作者Chayan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:23:27