问题
I have implemented an android search view.When the search view gains focus, the close button [x] at the right shows up.How to hide close button in android search view
回答1:
Change your theme like this:
For < v21:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarWidgetTheme">@style/AppTheme.WidgetTheme</item>
</style>
<style name="AppTheme.WidgetTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="searchViewCloseIcon">@android:color/transparent</item>
</style>
For v21 and above:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>
<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
<item name="closeIcon">@android:color/transparent</item>
</style>
Also check this for more customization.
回答2:
I think, if you use support libraries, the first part of @Anton advice may not work, so it would be sufficient to write in values/styles.xml something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="searchViewStyle">@style/SearchViewStyleAfterV21</item>
</style>
...
<style name="SearchViewStyleAfterV21" parent="Widget.AppCompat.SearchView">
<item name="closeIcon">@android:color/transparent</item>
</style>
回答3:
Have you tried this set 'x' button programicaly ?
In case you want do it programicaly, put a piece of code that hides "x" button in
onQueryTextListener
:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String query) {
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchViewIcon.setVisibility(View.GONE);
return true;
}
});
来源:https://stackoverflow.com/questions/31262503/hide-close-button-in-android-search-view