设置SearchView清除按钮颜色

时间:2015-08-24 09:09:19

标签: android android-support-library searchview

我正在使用onCreateOptionsMenu在工具栏中创建搜索结果,但无法使清除X按钮最初为白色。开始输入字母时变为白色。清理后它也会保持白色。

Example of clear button having the color of gray instead of white

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.responsible_menu, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); //TODO: May not be needed?

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String query)
        {
            mAdapter.updateUIWithFilter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText)
        {
            mAdapter.updateUIWithFilter(newText);
            return false;
        }
    });

    // Does not work! Still not white.
    ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setColorFilter(Color.argb(255, 255, 255, 255));

    searchClose.setAlpha(255);

    return true;
}

responsible_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:appcompat="http://schemas.android.com/apk/res-auto"
      xmlns:app="http://schemas.android.com/tools">

    <item
        android:id="@+id/menu_search"
        android:title="@string/search"
        appcompat:actionViewClass="android.support.v7.widget.SearchView"
        appcompat:showAsAction="always"/>

</menu>

6 个答案:

答案 0 :(得分:20)

找到解决方案。但是,对更好的人感兴趣。

以24pt白色从https://www.google.com/design/icons/#ic_clear下载“清除按钮”图片,并将此代码添加到onCreateOptionsMenu

的末尾
    // Does help!
    ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setImageResource(R.drawable.ic_clear_white_24dp);

答案 1 :(得分:3)

您也可以从XML设置它。我使用支持库24.2.1以及主题为&#34; Theme.AppCompat.Light&#34;的主题的活动。和ActionBar中的android.support.v7.widget.SearchView。

您可以在活动主题中设置actionBarWidgetTheme,如下所示:

<style name="ActivityTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarWidgetTheme">@style/MyActionBarWidgetTheme</item>
</style>

然后定义MyActionBarWidgetTheme并设置你想要的颜色:

<style name="MyActionBarWidgetTheme">
    <item name="android:textColorSecondary">@android:color/white</item>
</style>
  • android:textColorSecondary适用于关闭图标

  • android:textColorPrimary适用于您输入的文字

  • colorControlActivated适用于光标

  • android:textColorHint适用于提示文本

答案 2 :(得分:2)

尝试一下:

    ImageView searchClose = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

答案 3 :(得分:1)

您可以使用自己的自定义图标代替searchview默认取消图标。

我使用了以下代码,效果很好,希望能为您效劳。

private void setCloseSearchIcon(SearchView searchView) {
        try {
            Field searchField = SearchView.class.getDeclaredField("mCloseButton");
            searchField.setAccessible(true);
            ImageView closeBtn = (ImageView) searchField.get(searchView);
            closeBtn.setImageResource(R.drawable.ic_close);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

这将在api 14级以上工作。

  

科特林

private fun configureCloseButton(searchView: SearchView) {
    val searchClose =  searchView.javaClass.getDeclaredField("mCloseButton")
    searchClose.isAccessible = true
    val closeImage = searchClose.get(searchView) as ImageView
    closeImage.setImageResource(R.drawable.arrow_back_gray) // your image here
}

答案 4 :(得分:0)

有更好的解决方案。 如https://android-developers.googleblog.com/2014/10/appcompat-v21-material-design-for-pre.html“ SearchView窗口小部件”部分所述,只需使用以下内容即可:

<style name=”Theme.MyTheme” parent=”Theme.AppCompat”>
    <item name=”searchViewStyle”>@style/MySearchViewStyle</item>
</style>
<style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”>
    <!-- Background for the search query section (e.g. EditText) -->
    <item name="queryBackground">...</item>
    <!-- Background for the actions section (e.g. voice, submit) -->
    <item name="submitBackground">...</item>
    <!-- Close button icon -->
    <item name="closeIcon">...</item>
    <!-- Search button icon -->
    <item name="searchIcon">...</item>
    <!-- Go/commit button icon -->
    <item name="goIcon">...</item>
    <!-- Voice search button icon -->
    <item name="voiceIcon">...</item>
    <!-- Commit icon shown in the query suggestion row -->
    <item name="commitIcon">...</item>
    <!-- Layout for query suggestion rows -->
    <item name="suggestionRowLayout">...</item>
</style>

closeIcon是您要寻找的。

自定义关闭图标的示例(只需更改fillColor):

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#ffffff"
        android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

我在带有特定清单的可搜索活动和带有options_menu的{​​{1}}文件的app:actionViewClass="androidx.appcompat.widget.SearchView"文件的官方配置下对其进行了测试:https://developer.android.com/training/search/setup

答案 5 :(得分:0)

安卓版

ImageView ivClose = searchView.findViewById(androidx.appcompat.R.id.search_close_btn);

更改 res img

ivClose.setImageResource(R.drawable.ic_clear_white_24dp);

或者只是改变色调

ivClose.setColorFilter(ContextCompat.getColor(mContext, R.color.white), android.graphics.PorterDuff.Mode.SRC_IN);