如何更改searchview图标的颜色? [英] How to change searchview icon color?

查看:772
本文介绍了如何更改searchview图标的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

首先,由于我无法对其进行任何更改,因此searview图标似乎无法被代码更改.我的搜索视图在menu.xml中声明为

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.xml as this

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

在我的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;

我从一个特殊的类中获得的MenuTint.colorIcons(this, menu, Color.BLUE);我发现此处基本上发生了变化我所有的图标都变色了,很遗憾,不适用于searchview图标.

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

然后我发现了一些建议使用这种样式的答案

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>

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

然后,我发现我应该添加android-support-app-compat项目库而不是仅仅添加.jar.没用我的意思是,导入有效,但错误始终显示.然后,我发现某个地方应该将project.properties从target = android-19更改为21(或更高版本),但是它没有解决找不到资源"错误.

I then found that I should add the android-support-app-compat project 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.properties from target=android-19 to 21 (or higher) but it didn't solved the "No resource found" error.

因此,对于这个需要更改搜索视图颜色的简单细节,我深感困惑.

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>

但是我最终得到了

如您所见,此方法仅适用于我的自定义图标,而不适用于searchview图标,也不适用于菜单溢出图标.我想同样地更改所有图标之间的距离.

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的搜索图标,只需在视图内获取图像视图,如下所示:

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

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

重要的是图标是 R.id.search_button ,您可以将其替换为您提供的白色矢量素材,在这种情况下为 R.drawable.ic_search_icon >

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

类似地,您可以按以下方式更改SearchView中的文本:

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

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

这篇关于如何更改searchview图标的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆