Custom cursor color in SearchView

☆樱花仙子☆ 提交于 2019-12-29 00:25:31

问题


I want to change the color of the blinking cursor in a SearchView in the Actionbar (I'm using Actionbar Sherlock for compatibility). Until now I've managed to change the color of the cursor for a EditText element. This SO post helped me to accomplish that and the style I'm using for the EditText looks like this:

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

The red cursor looks like this:

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

I did take a look at the implementation of the SearchView widget and as I though it uses an AutoCompleteTextView for the text input. Since the AutoCompleteTextView is derived from the EditText widget, I don't really understand why the style is working for the EditText but not for the AutoCompleteTextView (and therefore for the SearchView).

Has anyone managed to change the color of the cursor in the SearchView widget?


回答1:


I just found a solution that works. I replaces the

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

with

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

and modified my theme so that it looks like this:

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


来源:https://stackoverflow.com/questions/15527420/custom-cursor-color-in-searchview

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