在SearchView中自定义光标颜色

5
我想改变Actionbar中SearchView闪烁光标的颜色(我使用Actionbar Sherlock以实现兼容性)。到目前为止,我已经成功地改变了EditText元素的光标颜色。这篇SO文章帮助我完成了这个过程,我正在使用的EditText样式如下:
<style name="CustomTheme" parent="Theme.Sherlock.Light">
    <item name="android:editTextStyle">@style/edittext_style</item>
</style>

<style name="edittext_style" parent="@android:style/Widget.EditText">
    <item name="android:textCursorDrawable">@drawable/red_cursor</item>
</style>

红色光标看起来像这样:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle" >
    <gradient android:startColor="@color/darkRed" 
              android:endColor="@color/darkRed" />
    <size android:width="1dp" />
</shape>

我确实查看了SearchView小部件的实现,发现它使用AutoCompleteTextView作为文本输入。由于AutoCompleteTextView是从EditText小部件派生的,我不太明白为什么EditText的样式可以工作,但AutoCompleteTextView(因此也包括SearchView)不能。

有人成功更改过SearchView小部件中光标的颜色吗?

1个回答

8
我发现了一个可行的解决方案。我替换了

<style name="edittext_style" parent="@android:style/Widget.EditText">
    <item name="android:textCursorDrawable">@drawable/red_cursor</item>
</style>

使用

<style name="SearchViewStyle" parent="Widget.Sherlock.Light.SearchAutoCompleteTextView">
    <item name="android:textCursorDrawable">@drawable/red_cursor</item>
</style>

我修改了我的主题,使其看起来像这样:

<style name="CustomTheme" parent="Theme.Sherlock.Light">
    ...
    <item name="searchAutoCompleteTextView">@style/SearchViewStyle</item>
    ...
</style>

你只需要在SearchAutoCompleteTextView主题上覆盖属性android:textCursorDrawable,那就可以了。 - MaTriXy
是的,那就是我做的。谢谢。 - mvieghofer
有没有可能更改默认的颜色,但不更改可绘制对象?我已经使用了colorControlActivated,但这会改变除文本插入符颜色之外的其他内容... - android developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接