【问题标题】:Toolbar SearchView cursor color工具栏 SearchView 光标颜色
【发布时间】:2016-09-16 11:41:25
【问题描述】:

你好。我正在尝试更改 SearchView 文本输入光标颜色,但遇到了很多困难。我发现它取决于我在 AppTheme 中定义的 colorAccent 值,但我无法更改它,因为它会影响很多其他 UI 元素。当我尝试使用自己的 colorAccent 参数为我的工具栏设置自定义样式时,没有任何变化,光标仍然是相同的颜色(非常接近工具栏,所以它似乎不可见)。

我的工具栏

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

我的搜索菜单

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

我的风格

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

活动代码

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

【问题讨论】:

标签: android searchview


【解决方案1】:

我有同样的问题,但我终于通过使用 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>

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

【讨论】:

  • 这可以用最少的代码很好地工作。谢谢你的回答。
【解决方案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!请不要把你的源代码扔在这里。友善一点,并尝试对您的答案进行漂亮的描述,以便其他人会喜欢并支持它。见:How do I write a good answer?
【解决方案3】:

当我尝试使用自己的 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" >

搜索样式

会这样吗?

【讨论】:

  • 感谢您的关注,我的代码中没有这个错误,我是在重命名样式和一些变量以便在此处发布时犯的
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 2015-11-21
    • 2017-02-07
    • 1970-01-01
    • 2015-02-28
    相关资源
    最近更新 更多