Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

android - Custom cursor color in SearchView

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...