工具栏中SearchView的光标颜色。

5

enter image description here Hello. I am trying to change SearchView text input cursor color and I'm facing a lot of difficulties. I've found that it depends on my colorAccent value defined in AppTheme, but I can't change it, because it will affect a lot of other UI elements. When I try to set custom style to my Toolbar with own colorAccent parameter, nothing changes, the cursor is still the same color (very close to the Toolbar one, so it seems to be invisible).

My Toolbar

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/newsfeed_toolbar"
        android:layout_width="match_parent"
        android:visibility="visible"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        style="@style/SearchStyle"
        app:titleTextAppearance="@style/toolbar_title_style"
        popupTheme="@style/AppTheme.PopupOverlay" >

    </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

My search menu

   <?xml version="1.0" encoding="utf-8"?>
   <menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">

   <item
    android:id="@+id/newsfeed_search"
    android:icon="@mipmap/ic_search"
    app:showAsAction="always|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>
    </menu>

My style

<style name="SearchStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar"> <item name="android:textCursorDrawable">@android:color/white</item> <item name="android:colorAccent">@android:color/white</item> </style>

Activity code

toolbar = (Toolbar) findViewById(R.id.newsfeed_toolbar);
setSupportActionBar(toolbar);
 @Override
public boolean onCreateOptionsMenu( Menu menu) {
    getMenuInflater().inflate( R.menu.menu, menu);

    MenuItem searchItem = menu.findItem( R.id.newsfeed_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    \\The next few lines is what I tried from other StackOverFlow responses, but no success    
final int textViewID = searchView.getContext().getResources().getIdentifier("android:id/search_src_text",null, null);
    final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(textViewID);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, android.R.color.white); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {}
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchWord = query;
            \\API Stuff

            return false;
        }
        @Override
        public boolean onQueryTextChange(String s) {
            if (s.length()>5) {
                \\API Stuff
            return false;
        }
    });
    MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            searchMode = true;
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            searchMode = false;
            searchWord = null;
            pageCount = 1;
            return true;
        }
    });
    return true;
}


尝试这个解决方案,它不是完美的,但应该可以工作 https://dev59.com/Z14c5IYBdhLWcg3wl7bm#34207795 - xklakoux
4个回答

19
我有同样的问题,但是最终通过使用 style 找到了解决方法。
将此项样式添加到您的 Activity Theme styles.xml 中。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="autoCompleteTextViewStyle">@style/cursorColor</item>
    </style>

<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
    <item name="android:textCursorDrawable">@drawable/cursor</item>
</style>

然后在drawable文件夹中添加cursor.xml
    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#FFFFFF" />
    <size android:width="1dp" />
</shape>

并且删除搜索样式,无论你在工具栏中设置了什么。


1
这个很好用,代码很简洁。感谢你的回答。 - Victor Okech

2
**Try this**

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_contacts, menu);

    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
    search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

    AutoCompleteTextView searchTextView = (AutoCompleteTextView) search.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, R.drawable.cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {
    }
    return super.onCreateOptionsMenu(menu);
}


**drawable/cursor.xml **

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <size android:width="2dp" />
</shape>

欢迎来到 Stack Overflow!请不要仅仅把你的源代码扔在这里。友好地尝试给出一个良好的描述,以便其他人会喜欢并点赞它。参见:如何撰写一个好的答案? - sɐunıɔןɐqɐp

0
当我尝试使用自己的colorAccent参数为工具栏设置自定义样式时,什么也没有改变。
我看到你在设置工具栏样式时有一个拼写错误。
 <android.support.v7.widget.Toolbar
    android:id="@+id/newsfeed_toolbar"
    android:layout_width="match_parent"
    android:visibility="visible"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    style="@style/SearcStyle"
    app:titleTextAppearance="@style/toolbar_title_style"
    popupTheme="@style/AppTheme.PopupOverlay" >

搜索样式

这可能是这种情况吗?


感谢您的关注,这个错误在我的代码中并不存在,我是在重命名样式和一些变量以发布到这里时犯了这个错误。 - inthy

0

这是 Android 的组件。实现方式不同。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:autoCompleteTextViewStyle">@style/searchViwStyle</item>
</style>

<style name="searchViwStyle" parent="Widget.AppCompat.AutoCompleteTextView">
    <item name="android:textCursorDrawable">@drawable/color_cursor</item>
</style>

还有drawable/color_cursor

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
   <size android:width="2dp" />
   <solid android:color="@color/cursor_color"  />
</shape>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接