【问题标题】:How to delete or change the searchview icon inside the SearchView actionBar?如何删除或更改 SearchView actionBar 内的 searchview 图标?
【发布时间】:2015-04-14 17:38:47
【问题描述】:

如何删除或更改编辑文本中的搜索视图图标? 我正在使用 Appcompat 库。

我使用下面的代码来修改和删除,但它不起作用:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    View search_mag_icon = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
        search_mag_icon.setBackgroundResource(R.drawable.ic_stub);
        //search_mag_icon.setVisibility(View.GONE);

【问题讨论】:

    标签: android android-actionbar searchview android-appcompat


    【解决方案1】:

    终于找到了解决办法。请尝试以下操作:

    try {
        Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
        mDrawable.setAccessible(true);
        Drawable drawable =  (Drawable)mDrawable.get(your search view here);
        drawable.setAlpha(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 在尝试了一切之后,这是唯一对我有用的解决方案,谢谢
    • 添加这一行 drawable.setBounds(0,0,0,0) 使您可以使用 0 左填充显示提示,否则提示将显示在仍然透明存在的图标之后.
    • 它有效,但显示警告 检测到对字段 mSearchHintIcon 的反射访问以及不存在或不可见的方法
    【解决方案2】:

    根据AppCompat v21 blog post,您可以为您的SearchView 提供自定义样式,覆盖搜索图标等内容:

    <style name="Theme.MyTheme" parent="Theme.AppCompat">
        <item name="searchViewStyle">@style/MySearchViewStyle</item>
    </style>
    <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
        <!-- Search button icon -->
        <item name="searchIcon">@drawable/custom_search_icon</item>
    </style>
    

    【讨论】:

    • 他想从提示中完全删除这个图标,而不是替换它。
    • @Solace - 用任何内容替换它 == 删除它
    • 我试过&lt;item name="searchIcon"&gt;&lt;/item&gt;,它给了我String types not allowed (at 'searchIcon' with value ''). 如果你能在这方面给我一些指导,我将非常感激?你自己试过吗?
    • @Solace - 你必须使用@null,而不仅仅是空白。
    • 对我不起作用 =( 实际上,这些样式都没有被应用,发布了一个问题here
    【解决方案3】:

    不建议使用私有方法作为SearchView.class.getDeclaredField("mSearchHintIcon");

    <item android:id="@+id/menu_search"
            android:icon="@drawable/action_search"
            app:actionLayout="@layout/xx_layout"
            app:showAsAction="always"/>
    

    在 xx_layout 中:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/search_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:searchHintIcon="@null"
        android:iconifiedByDefault="true" />
    

    【讨论】:

    • 关于风格,@null@null 可以解决问题。
    【解决方案4】:

    您可以使用以下代码行:

    int searchMagIcon = getResources().getIdentifier("search_mag_icon", "id", "android");
    ImageView SearchHintIcon = (ImageView) searchView.findViewById(searchMagIcon);
    

    这将保存 ImageView,然后您可以更改它。

    【讨论】:

      【解决方案5】:

      我使用了带有属性app:actionViewClass="android.support.v7.widget.SearchView"的菜单项

      关于我设置的活动:

      searchView = (SearchView) searchMenuItem.getActionView();
      searchView.setIconifiedByDefault(false);
      

      要删除我所做的图标:

      ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
      searchIcon.setImageDrawable(null);
      

      【讨论】:

        【解决方案6】:

        remove 是您在 xml 中提到的正确可绘制对象:

        final Drawable remove = ContextCompat.getDrawable(getActivity(), R.drawable.ic_close_circle);
                etSearchProducts.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent event) {
                        if (etSearchProducts.getCompoundDrawables()[2] == null) {
                            return false;
                        }
                        if (event.getAction() != MotionEvent.ACTION_UP) {
                            return false;
                        }
                        if (event.getX() > etSearchProducts.getWidth() - etSearchProducts.getPaddingRight() - remove.getIntrinsicWidth()) {
                            etSearchProducts.setText("");
                            Utility.hideKeyboard(getActivity());
                        }
        
                        return false;
                    }
                });
        

        【讨论】:

          【解决方案7】:

          最简单的方法是

          1. 如果您使用的是AppCompat,请使用

                app:iconifiedByDefault="false"
                app:searchIcon="@null"
            
          2. 在上述代码中使用android 代替app

            android:iconifiedByDefault="false"
            android:searchIcon="@null"
            

          【讨论】:

            【解决方案8】:

            我研究了很多,最后我找到了这个解决方案,它对我有用!
            你可以用这个

            如果你使用 appcompat 试试这个:

            ImageView searchIconView = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
            searchIconView.setImageResource(R.drawable.yourIcon);
            

            如果你使用 androidx 试试这个:

            ImageView searchIconView = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_button);
                searchIconView.setImageResource(R.drawable.yourIcon);
            

            它将更改默认的 serchview 图标。

            希望对你有帮助!

            【讨论】:

            • 只是为了让您知道,针对不同问题发布多个相同的答案将被自动标记为版主审核。如果您确实遇到了一个答案可以同时回答两个问题的问题,您应该考虑将其中一个问题标记为另一个问题的重复项。
            【解决方案9】:

            只需在 Searchview 中将 searchHintIcon 属性设置为 null。

            app:searchHintIcon="@null"

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-04-24
              • 1970-01-01
              • 2012-12-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-07-08
              相关资源
              最近更新 更多