进行了很多实验后,我最终能够使用autoCompleteTextViewStyle
属性和自定义光标Drawable
来改变光标颜色。修改您在示例中提供的代码,您可以这样做。首先,您将如下所示将上述属性添加到您的主题中:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="autoCompleteTextViewStyle">@style/cursorColor</item>
</style>
接下来,您需要创建 "cursorColor" 的样式,该样式将引用您将要创建的光标 Drawable
(下一步):
<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/cursor</item>
</style>
最后要做的是创建光标可绘制对象(cursor.xml),它将用作替换光标:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<size android:width="1dp" />
</shape>
以下是应用新主题前后光标的样子:
应用前
应用后
最后需要注意的是,你可以通过修改Drawable
中相应的字段来随时更改新光标的宽度和颜色:
<!-- Change this to increase the width -->
<size android:width="1dp"/>
<!-- Change this to change the color -->
<solid android:color="#FFFFFF" />