2016-02-01 15 views
4

Android的搜索視圖光標不是同色的工具欄的可見

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="right|end"> 

     <android.support.v7.widget.SearchView 
      android:background="@null" 
      android:id="@+id/searchView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
</android.support.v7.widget.Toolbar> 

我colorAccent也colorPrimary,如何改變光標顏色,我提到其他quetions,但沒有解決我的問題

回答

10

你有兩個選項。

1)。您可以在styles.xml中使用這些屬性來更改光標顏色。

<item name="colorControlNormal">@color/colorEditextDisabled</item> 
<item name="colorControlActivated">@color/colorEditextEnabled</item> // change this color to the required cursor color your need. 
<item name="colorControlHighlight">@color/colorEditextEnabled</item> 

2)。SearchView中找到EditText,並將android:textCursorDrawable屬性設置爲@null。這將導致android:textColor作爲遊標顏色。但是,由於您無法以編程方式設置textCursorDrawable,因此您必須在此情況下使用反射。

EditText etSearch= ((EditText) yourSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)); 
     try { 
      Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); 
      f.setAccessible(true); 
      f.set(etSearch, null);// set textCursorDrawable to null 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     etSearch.setTextColor(ContextCompat.getColor(mContext, R.color.yourColor)); 

希望以上任何解決方案都適合您.. !!

+1

我不能傳遞null mCursorDrawableRes ......這個期望int值 –

3

使用colorAccent在style.xml ...

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/orange</item> 
    <item name="colorPrimaryDark">@color/dark_orange</item> 
    <item name="colorAccent">@color/blue</item> 
</style>