问题
I've used "Android Action Bar Style Generator" in order to generate my own Holo theme. Then I've added two icons to the action bar, one for a searchFilter and one for a file picker, both icons are white. File picker icon (at the right) looks good, but somehow search icon looks dark grey. When typing on the search text view, close button also looks dark grey.
I've tried changing color of the actionbar and also from the searchview, without success. Why is the search view showing this color? How can I change it?

Thank you very much.
UPDATE: I thought that my app was using my icon but it doesn't, it uses the default search view icon, so my question actually is, how do I change the default icon of the search view? This sentence at the menu.xml file is not working:
android:icon="@drawable/selectable_header_search"
回答1:
Unfortunately, there is no easy way to change the SearchView
icon to a custom drawable, since the theme attribute searchViewSearchIcon
is not public. See this answer for details.
However, I think that your problem is caused by inheriting from the wrong theme. Please use android:Theme.Holo.Light.DarkActionBar
as the base for your theme. Then the default icons on the action bar should have a light color.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
...
</style>
回答2:
Since the searchViewSearchIcon attribute is not public, you can change the icon using the following code:
int searchIconId = searchView.getContext().getResources().getIdentifier("android:id/search_button",null, null);
ImageView searchIcon = (ImageView) searchView.findViewById(searchIconId);
searchIcon.setImageResource(R.drawable........);
回答3:
Try to set up in the showAsAction this option: *MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW*
Without this option the SearchView do not take the icon which you setup for action button.
menu.add("Search")
.setIcon(
getResources().getDrawable(
R.drawable.selectable_header_search))
.setActionView(searchView)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
回答4:
The easiest way I've found to change the default searchview icon in the action bar is in your onPrepareOptionsMenu. You can see my answer here to a similar question!
回答5:
In your theme style include this:
<style name="YourTheme.Toolbar">
<item name="searchViewStyle">@style/YourActionBarSearch</item>
<item name="editTextColor">@color/your_ab_text_color</item>
<item name="android:textColorHint">@color/your_ab_hint_color</item>
</style>
You can now define the style of the SearchView with searchViewStyle
, and editTextColor
will set the color of the text in the search box. By setting android:textColorHint
you will also set the color of the hint, e.g. "Search...".
<style name="YourActionBarSearch" parent="@style/Widget.Holo.SearchView">
<item name="searchIcon">@drawable/ic_ab_search</item>
<item name="queryBackground">@null</item>
<item name="submitBackground">@null</item>
<item name="searchHintIcon">@drawable/ic_ab_small_search</item>
<item name="defaultQueryHint">@string/toolbar_search_hint</item>
<item name="closeIcon">@drawable/ic_ab_small_search_close</item>
</style>
This will style your search view as you like, more or less. The field searchIcon
is the icon in the action bar. The field searchHintIcon
is the small icon to the left of the hint in the search field. The field closeIcon
is the close icon at the right side of the search field.
Check this out, for more style settings:
$ANDROID_SDK/platforms/android-24/data/res/values/styles_holo.xml
回答6:
For those like me hoping to find a way to truly change the icon, you can use this with actionbar sherlock:
// Getting the 'search_icon'
ImageView searchIcon = (ImageView)searchView.findViewById(R.id.abs__search_button);
// Setting background of 'search_plate' to earlier defined drawable.
searchIcon.setImageResource(R.drawable.search_button_selector);
You can't change the default one with the common way android:icon
, because the field related to it had been set to private.
来源:https://stackoverflow.com/questions/14368788/how-to-change-searchview-default-icon