【问题标题】:android search view cursor not visible as same color of toolbarandroid搜索视图光标与工具栏的颜色相同不可见
【发布时间】:2016-02-01 18:57:46
【问题描述】:

    <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,如何更改光标颜色,我参考了其他问题,但没有解决我的问题

【问题讨论】:

    标签: android search


    【解决方案1】:

    你有两个选择。

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

    希望上述任何解决方案都对您有用..!!

    【讨论】:

    • 我无法为 mCursorDrawableRes 传递 null ...这需要一个 int 值
    【解决方案2】:

    在 style.xml 中使用 colorAccent...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 2016-06-30
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多