Hide close button in android Search View

此生再无相见时 提交于 2019-12-06 12:27:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!