如何更改 SearchView 上的默认图标,以在 Android 的操作栏中使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10445760/
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
How to change the default icon on the SearchView, to be use in the action bar on Android?
提问by Valdemar
I'm having a bit of trouble customizing the search icon in the SearchView. On my point of view, the icon can be changed in the Item attributes, right? Just check the code bellow..
我在自定义 SearchView 中的搜索图标时遇到了一些麻烦。在我看来,图标可以在 Item 属性中更改,对吗?只需检查下面的代码..
Can someone tell me what I'm doing wrong?
有人能告诉我我做错了什么吗?
This is the menu I'm using, with my custom search icon icn_lupa. But when I run the app, I always get the default search icon...
这是我正在使用的菜单,带有我的自定义搜索图标icn_lupa。但是当我运行应用程序时,我总是得到默认的搜索图标......
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:title="@string/menu_search"
android:icon="@drawable/icn_lupa"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
Thanks in advance.
提前致谢。
采纳答案by Snicolas
It looks like the actionViewClass overides the icon and it doesn't look like you can change it from this class.
看起来 actionViewClass 覆盖了图标,而且您似乎无法从此类更改它。
You got two solutions:
你有两个解决方案:
- Live with it and I think it's the best option in terms of user experience and platform conformity.
- Define your own actionViewClass
- 接受它,我认为它是用户体验和平台一致性方面的最佳选择。
- 定义你自己的 actionViewClass
回答by just_user
I've found another way to change the search icon which goes in the same line as Diego Pino's answer but straight in onPrepareOptionsMenu
.
我找到了另一种更改搜索图标的方法,该图标与 Diego Pino 的答案位于同一行,但直接插入onPrepareOptionsMenu
.
In your menu.xml (same as before)
在您的 menu.xml 中(与以前相同)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_fav"
android:title="@string/action_websearch"
android:showAsAction="always|never"
android:actionViewClass="android.widget.SearchView" />
</menu>
In your activity:
在您的活动中:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem searchViewMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchViewMenuItem.getActionView();
int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
ImageView v = (ImageView) mSearchView.findViewById(searchImgId);
v.setImageResource(R.drawable.your_new_icon);
mSearchView.setOnQueryTextListener(this);
return super.onPrepareOptionsMenu(menu);
}
I followed the example for changing the edittext in this example.
You should be able to do this for all icons/backgrounds in your SearchView
, to find the right ID you can check here.
您应该能够对 中的所有图标/背景执行此操作SearchView
,以找到您可以在此处查看的正确 ID 。
Hope this helps someone else as well!
希望这对其他人也有帮助!
UPDATE November 2017:
2017 年 11 月更新:
Since this answer android has been updated with the possibility of changing the search icon through the XML.
由于此答案 android 已更新,可以通过 XML 更改搜索图标。
If you target anything below android v21 you can use:
如果您针对 android v21 以下的任何内容,您可以使用:
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:searchIcon="@drawable/ic_search_white_24dp"
app:closeIcon="@drawable/ic_clear_white_24dp" />
Or v21 and later:
或 v21 及更高版本:
<SearchView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:searchIcon="@drawable/ic_search_white_24dp"
android:closeIcon="@drawable/ic_clear_white_24dp" />
And there are even more options:
还有更多选择:
closeIcon
commitIcon
goIcon
searchHintIcon
searchIcon
voiceIcon
回答by patrickjason91
Nice answer from @just_user
来自@just_user 的好答案
For my case, since I am using the appcompat v7 library for the SearchView + ActionBar, i modified his solution a bit to make it compatible to my project, it should work so as long as you did not modify anything when you added appcompat v7 as library
就我而言,由于我将 appcompat v7 库用于 SearchView + ActionBar,因此我稍微修改了他的解决方案以使其与我的项目兼容,只要在添加 appcompat v7 时没有修改任何内容,它就应该可以工作图书馆
XML:
XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:metrodeal="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/main_menu_action_search"
android:orderInCategory="100"
android:title="@string/search"
metrodeal:showAsAction="always"
metrodeal:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/search_btn"/>
</menu>
Java code:
爪哇代码:
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem searchViewMenuItem = menu.findItem(R.id.main_menu_action_search);
SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchViewMenuItem);
int searchImgId = android.support.v7.appcompat.R.id.search_button; // I used the explicit layout ID of searchview's ImageView
ImageView v = (ImageView) mSearchView.findViewById(searchImgId);
v.setImageResource(R.drawable.search_btn);
super.onPrepareOptionsMenu(menu);
}
Excuse for the very big icon (I have not resized the icon just yet), but it should work as it is.
原谅这个非常大的图标(我还没有调整图标的大小),但它应该可以正常工作。
回答by Mathieu Post
I was struggling with this too but then I accidentaly used 'collapseActionView' and that fixed it! My menu.xml looks like this now:
我也在为此苦苦挣扎,但后来我不小心使用了“collapseActionView”并修复了它!我的 menu.xml 现在看起来像这样:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:title="@string/menu_search"
android:showAsAction="always|withText|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:icon="@android:drawable/ic_menu_search" />
</menu>
The downside of this is that on tablets the SearchView will appear on the left side of the ActionBar instead of where the searchicon is, but I don't mind that.
这样做的缺点是,在平板电脑上,SearchView 将出现在 ActionBar 的左侧,而不是搜索图标所在的位置,但我不介意。
回答by woodyhou
I defined a style to do it .
我定义了一种风格来做到这一点。
here is my xml:
这是我的 xml:
<android.support.v7.widget.SearchView
android:id="@+id/sv_search"
android:icon="@android:drawable/ic_menu_search"
android:layout_width="fill_parent"
**style="@style/CitySearchView"**
android:layout_height="wrap_content"/>
and this is my style:
这是我的风格:
<style name="CitySearchView" parent="Base.Widget.AppCompat.SearchView">
<item name="searchIcon">@drawable/ic_more_search</item>
</style>
That it!
那个!
After finish that,just take a look at Base.Widget.AppCompat.SearchView.
完成后,看看Base.Widget.AppCompat.SearchView。
<style name="Base.Widget.AppCompat.SearchView" parent="android:Widget">
<item name="layout">@layout/abc_search_view</item>
<item name="queryBackground">@drawable/abc_textfield_search_material</item>
<item name="submitBackground">@drawable/abc_textfield_search_material</item>
<item name="closeIcon">@drawable/abc_ic_clear_mtrl_alpha</item>
<item name="searchIcon">@drawable/abc_ic_search_api_mtrl_alpha</item>
<item name="goIcon">@drawable/abc_ic_go_search_api_mtrl_alpha</item>
<item name="voiceIcon">@drawable/abc_ic_voice_search_api_mtrl_alpha</item>
<item name="commitIcon">@drawable/abc_ic_commit_search_api_mtrl_alpha</item>
<item name="suggestionRowLayout">@layout/abc_search_dropdown_item_icons_2line</item>
</style>
every item can be override by define a new style .
每个项目都可以通过定义新样式来覆盖。
Hope it helps!
希望能帮助到你!
回答by Diego Pino
There's a way to do this. The trick is to recover the ImageView using its identifier and setting a new image with setImageResource()
. This solution is inspired on Changing the background drawable of the searchview widget.
有一种方法可以做到这一点。诀窍是使用其标识符恢复 ImageView 并使用setImageResource()
. 此解决方案的灵感来自更改 searchview 小部件的背景可绘制对象。
private SearchView searchbox;
private void customizeSearchbox() {
setSearchHintIcon(R.drawable.new_search_icon);
}
private void setSearchHintIcon(int resourceId) {
ImageView searchHintIcon = (ImageView) findViewById(searchbox,
"android:id/search_mag_icon");
searchHintIcon.setImageResource(resourceId);
}
private View findViewById(View v, String id) {
return v.findViewById(v.getContext().getResources().
getIdentifier(id, null, null));
}
回答by SumeetP
SearchView searchView = (SearchView) findViewById(R.id.address_search);
SearchView searchView = (SearchView) findViewById(R.id.address_search);
try {
Field searchField = SearchView.class
.getDeclaredField("mSearchButton");
searchField.setAccessible(true);
ImageView searchBtn = (ImageView) searchField.get(searchView);
searchBtn.setImageResource(R.drawable.search_glass);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
回答by znat
After some research I found the solution here. The trick is that the icon is not in an ImageView
but in the Spannable
object.
经过一番研究,我在这里找到了解决方案。诀窍是图标不在对象中,ImageView
而是在Spannable
对象中。
// Accessing the SearchAutoComplete
int queryTextViewId = getResources().getIdentifier("android:id/search_src_text", null, null);
View autoComplete = searchView.findViewById(queryTextViewId);
Class<?> clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
SpannableStringBuilder stopHint = new SpannableStringBuilder(" ");
stopHint.append(getString(R.string.your_new_text));
// Add the icon as an spannable
Drawable searchIcon = getResources().getDrawable(R.drawable.ic_action_search);
Method textSizeMethod = clazz.getMethod("getTextSize");
Float rawTextSize = (Float)textSizeMethod.invoke(autoComplete);
int textSize = (int) (rawTextSize * 1.25);
searchIcon.setBounds(0, 0, textSize, textSize);
stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Set the new hint text
Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
setHintMethod.invoke(autoComplete, stopHint);
回答by TomTaila
<SearchView
android:searchIcon="@drawable/ic_action_search"
..../>
use the searchIcon xml tag
使用 searchIcon xml 标签
回答by Razel Soco
for api level < 21, i did this:
对于 api 级别 < 21,我这样做了:
int searchImgId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView ivIcon = (ImageView) searchView.findViewById(searchImgId);
if(ivIcon!=null)
ivIcon.setImageResource(R.drawable.ic_search);
from this
由此
to this
对此