eclipse 如何更改搜索视图图标颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44855532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:26:33  来源:igfitidea点击:

How to change searchview icon color?

androideclipseandroid-actionbariconssearchview

提问by Marcus Casagrande

I know there's already a lot of answers for this question here, and I wouldn't be creating another thread if I hadn't already tried everything I saw here. Anyway, the question is pretty simple. I don't mind if the solution comes from XML styling or Java code (both solutions are welcome). I will show everything (most of it anyway) I've tried to solve it.

我知道这里已经有很多关于这个问题的答案,如果我还没有尝试过我在这里看到的所有内容,我就不会创建另一个线程。总之,这个问题很简单。我不介意解决方案是来自 XML 样式还是 Java 代码(这两种解决方案都受欢迎)。我将展示我尝试解决的所有内容(无论如何大部分内容)。

First of all, the searview icon seems to be immutable by code since I can't change anything from it. My searchview is declared in menu.xmlas this

首先,searview 图标似乎无法通过代码更改,因为我无法对其进行任何更改。我的搜索视图声明menu.xml如下

<item android:id="@+id/icon_search"
    android:title="Searchador"
    android:icon="@drawable/ic_search_black_24dp" <!-- icon not used by searchview anyway -->
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"
/>

and inside my onCreateOptionsMenuI have this

在我的里面我onCreateOptionsMenu有这个

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.icon_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
MenuTint.colorIcons(this, menu, Color.BLUE);
return true;

The MenuTint.colorIcons(this, menu, Color.BLUE);I got from a special class I found herewhere it basically changes all my icons color, unfortunately not working for the searchview icon.

MenuTint.colorIcons(this, menu, Color.BLUE);我从一个特殊的班级,我发现了这里它基本上改变了所有我的图标颜色,不幸的是没有工作的搜索查看图标。

I then found some answers suggesting to use a style like this

然后我找到了一些建议使用这样的风格的答案

<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
   <!-- changing my icon, its color and stuff.. -->
   <item name="searchIcon">@drawable/ic_search</item></style>

but I was getting the No resource found that matches the given name 'Widget.AppCompat.SearchView'.error.

但我收到了No resource found that matches the given name 'Widget.AppCompat.SearchView'.错误。

I then found that I should add the android-support-app-compatproject library instead of just the .jar. It didn't work. I mean, the import worked, but the error kept showing. Then, I found somewhere that I should change the project.propertiesfrom target=android-19 to 21 (or higher) but it didn't solved the "No resource found" error.

然后我发现我应该添加android-support-app-compat项目库而不仅仅是.jar. 它没有用。我的意思是,导入有效,但错误一直显示。然后,我发现我应该将project.propertiestarget=android-19 更改为 21(或更高),但它没有解决“找不到资源”错误。

So I'm pretty much stuck for this simple detail where I need to change the searchview color.

所以我几乎被这个简单的细节困住了,我需要改变搜索视图的颜色。

Also, this is not exactly the same question, but I believe it might be solved the in the same way so I will include here: I wanted to change distance between the icons. Someone suggested this solution

此外,这不是完全相同的问题,但我相信它可能会以相同的方式解决,因此我将在此处包括:我想更改图标之间的距离。有人提出了这个解决方案

<style name="ActionButtonStyle" parent="AppBaseTheme">
    <item name="android:minWidth">0dip</item>
    <item name="android:paddingLeft">0dip</item>
    <item name="android:paddingRight">0dip</item>                  
</style>

but I end up getting this

但我最终得到了这个

As you can see, this approach works for my custom icons only, but not for the searchview icon, neither for the menu overflow icon. I would like to change the distance between ALL icons equally.

如您所见,此方法仅适用于我的自定义图标,不适用于 searchview 图标,也不适用于菜单溢出图标。我想平等地改变所有图标之间的距离。

回答by tfad334

For android.x,

对于android.x,

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    ...

    ImageView searchIcon= 
    searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);

    // To change color of close button, use:
    // ImageView searchCloseIcon = (ImageView)searchView
    //        .findViewById(androidx.appcompat.R.id.search_close_btn);

    searchIcon.setColorFilter(getResources().getColor(R.color.colorPrimary),
            android.graphics.PorterDuff.Mode.SRC_IN);

    ...

    return true;

}

回答by Carlos Daniel

If you want to change SearchView's search icon, you just need to get the image view within the view as follow:

如果你想改变 SearchView 的搜索图标,你只需要在视图中获取图像视图,如下所示:

searchView = v.findViewById(R.id.searchView);
//change icon color
ImageView searchIcon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIcon.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_search_icon));

It is important that the icon is R.id.search_buttonand you can replace it with a white vector asset that you provide, in this case R.drawable.ic_search_icon

重要的是图标是R.id.search_button,你可以用你提供的白色矢量资源替换它,在这种情况下是R.drawable.ic_search_icon

Similarly, you can change the text within the SearchView as follows:

同样,您可以按如下方式更改 SearchView 中的文本:

//set color to searchview
SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(getResources().getColorandroid.R.color.white));
searchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));

回答by adougies

To change the search view's icon color use the following lines of code:

要更改搜索视图的图标颜色,请使用以下代码行:

SearchView searchView = (SearchView) findViewById(R.id.searchview);
ImageView icon = searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
icon.setColorFilter(Color.BLACK);

回答by Naseer Attari

 final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
        // change close icon color
        ImageView iconClose = (ImageView) searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
        iconClose.setColorFilter(getResources().getColor(R.color.grey));
       //change search icon color
        ImageView iconSearch = searchViewAndroidActionBar.findViewById(android.support.v7.appcompat.R.id.search_button);
        iconSearch.setColorFilter(getResources().getColor(R.color.grey));

回答by android developer

Take the Drawable you want to tint, wrap it with a tinted drawable , and set it as a new one for the search menu item:

取你想要着色的 Drawable,用一个着色的 drawable 包裹它,并将它设置为搜索菜单项的一个新的:

@JvmStatic
fun getTintedDrawable(inputDrawable: Drawable, @ColorInt color: Int): Drawable {
    val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
    DrawableCompat.setTint(wrapDrawable, color)
    DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
    return wrapDrawable
}

Usage:

用法:

val drawable =getTintedDrawable(...)
searchMenuItem.icon = drawable

And this should work for all Android versions that android-x (support library) supports.

这应该适用于 android-x(支持库)支持的所有 Android 版本。