2013-03-20 135 views
5

我想更改操作欄(我使用Actionbar Sherlock兼容性)中的SearchView中閃爍光標的顏色。直到現在我已經設法改變EditText元素的光標顏色。 This SO post幫助我實現這個目標,我使用了的EditText樣式看起來是這樣的:在SearchView中自定義光標顏色

<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小部件中光標的顏色?

回答

7

我剛剛發現了一個可行的解決方案。我取代了

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

你只需要重寫屬性的android:textCursorDrawable在SearchAutoCompleteTextView主題應該有效。 – MaTriXy 2013-03-22 06:37:38

+0

是的,這就是我所做的。謝謝 – mvieghofer 2013-03-24 09:52:30

+0

是否可以更改默認的顏色,但不能繪製?我已經使用了colorControlActivated,但這不僅僅改變了文字顏色...... – 2015-08-12 06:59:45