【问题标题】:Remove the SearchIcon as hint in the searchView在 searchView 中删除 SearchIcon 作为提示
【发布时间】:2013-12-17 21:45:57
【问题描述】:

我正在做一个应用程序,例如当我点击图像时(它是一个 searchView)

搜索板打开!

看起来像

但这里会显示默认的搜索图标(放大镜),但一旦输入一些文本,它就会消失

但我不希望即使第一次点击图像也显示放大镜

这里我没有使用任何 xml 文件

我的代码是

public class MainActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout relative = new RelativeLayout(this);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        relative.setLayoutParams(params);
        setContentView(relative);

        SearchView searchView = new SearchView(this);
        traverseView(searchView, 0);
//      searchView.setIconifiedByDefault(false);
        LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//      searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        searchView.setLayoutParams(searchViewparams);
        relative.addView(searchView);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    private void traverseView(View view, int index) {
        if (view instanceof SearchView) {
            SearchView v = (SearchView) view;
            for(int i = 0; i < v.getChildCount(); i++) {
                traverseView(v.getChildAt(i), i);
            }
        } else if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for(int i = 0; i < ll.getChildCount(); i++) {
                traverseView(ll.getChildAt(i), i);
            }
        } else if (view instanceof EditText) {
            ((EditText) view).setTextColor(Color.GREEN);
            ((EditText) view).setHintTextColor(Color.BLACK);
        } else if (view instanceof TextView) {
            ((TextView) view).setTextColor(Color.BLUE);
        } else if (view instanceof ImageView) {
            ((ImageView) view).setImageResource(R.drawable.ic_launcher);
        } else {
            Log.v("View Scout", "Undefined view type here...");
        }
    }
}

【问题讨论】:

  • 请发布您的代码和xml文件。
  • 发布代码@SiddharthVyas

标签: android android-image searchview


【解决方案1】:

在我的应用程序中,我使用了 android.support.v7.widget.SearchView 并隐藏搜索图标,这对我有用:

<android.support.v7.widget.SearchView
    ...
    app:iconifiedByDefault="false"
    app:searchIcon="@null"
/>

【讨论】:

  • 这对我有用。冗长的讨论让我担心这个解决方案与不同硬件的兼容性如何,但它是迄今为止最干净的。
  • 编程方式也是可能的:searchView.setIconifiedByDefault(false);
【解决方案2】:

我也遇到过这个问题。我将找到的教程和在 stackoverflow 中找到的现有答案结合在一起。

int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView magImage = (ImageView) searchView.findViewById(magId);
magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

注意searchView是一个LinearLayout,所以使用LinearLayout.LayoutParams来避免异常。

我也试过这个,但它不会删除视图。我似乎无法弄清楚为什么。:

magImage.setVisibility(View.GONE);

其他需要更改的视图,请参考to this tutorial.

【讨论】:

  • 我在 magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0)); 上得到 NPE,这意味着 magImage 为空,尽管我不知道为什么 :-(
  • 但在 v7.SearchWidget 中我们无法通过此方法隐藏搜索提示图标。只需在获得 SearchView 实例的地方做一件事,只需检查您是否没有编写 searchView.setIconifiedByDefault(false);方法
  • 应用兼容see
  • 除了setVisibility(View.GONE); 之外,不写searchView.setIconifiedByDefault(false); 也对我有用。奇怪的行为。
  • 我使用了 android.support.v7.appcompat.SearchView。为了解决隐藏它我使用了:searchView.setIconifiedByDefault(false); ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon); searchIcon.setImageDrawable(null);
【解决方案3】:

我只是把 @null 放到searchHintIcon 属性中:

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="searchHintIcon">@null</item>
</style>

并将该样式应用到我的应用主题中

<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
    <item name="searchViewStyle">@style/SearchViewMy</item>
</style>

为我工作。

【讨论】:

  • 简单明了。
【解决方案4】:

试试这三行:

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

*它仅适用于 android.widget.SearchView

【讨论】:

    【解决方案5】:

    TL;DR:只需将EditText 的提示设置为空字符串即可移除提示图标。

    我找到了一个真正有效的答案。有很多关于使用反射更改图标的内容 - 例如在 Styling the ActionBar SearchView 中讨论的内容。这个网站是一个很好的资源,但不幸的是搜索提示ImageView 没有改变(即使反射不会导致错误)。我能够删除此图像的唯一方法是:

    try {
        int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
        searchPlate.setHint("");
    }
    catch (Throwable t)
    {
        t.printStackTrace();
    }
    

    您应该将它放在您的 onCreateOptionsMenu 方法中,并使用以下方式获取对您的 XML-declared SearchView 的引用:

    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    

    【讨论】:

      【解决方案6】:

      我刚遇到同样的问题,这里有一些参考链接。

      http://blog.luxteam.net/2013/11/04/styling-appcompat-searchview/

      https://android.googlesource.com/platform/frameworks/support.git/+/android-support-lib-19.1.0/v7/appcompat/src/android/support/v7/widget/SearchView.java

      关键在下面的函数中,它将图标设置为ImageSpan。

      private CharSequence getDecoratedHint(CharSequence hintText) {
          // If the field is always expanded, then don't add the search icon to the hint
          if (!mIconifiedByDefault) return hintText;
      
          SpannableStringBuilder ssb = new SpannableStringBuilder("   "); // for the icon
          ssb.append(hintText);
          Drawable searchIcon = getContext().getResources().getDrawable(getSearchIconId());
          int textSize = (int) (mQueryTextView.getTextSize() * 1.25);
          searchIcon.setBounds(0, 0, textSize, textSize);
          ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          return ssb;
      }
      

      【讨论】:

        【解决方案7】:

        简单的解决方法是searchHintIcon属性

        <style name="SearchViewStyleMyApp" parent="Widget.AppCompat.SearchView">
                <!-- Background for the search query section (e.g. EditText) -->
                <item name="queryBackground">@android:color/white</item>
                <!-- note that this is how you style your hint icon -->
                <item name="searchHintIcon">@null</item>
                <!-- The hint text that appears when the user has not typed anything -->
                <item name="queryHint">@string/search_hint</item>
            </style>
        

        【讨论】:

          【解决方案8】:

          对于androidx你可以这样做

          ImageView magImage = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
          magImage.setVisibility(View.GONE);
          magImage.setImageDrawable(null);
          

          这对我有用

          【讨论】:

            【解决方案9】:

            为此在您的样式中使用“searchHintIcon”属性:

            <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
                <item name="searchViewStyle">@style/AppTheme.SearchView</item>
            </style>
            
            <style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
                <item name="searchHintIcon">@null</item>
            </style>
            

            您可以将@null 替换为您想要的任何可绘制资源

            【讨论】:

              【解决方案10】:

              这是答案

              ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
              
              ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
              linearLayoutSearchView.removeView(searchViewIcon);
              

              【讨论】:

              • 在较旧的 android 中,当您将 searchIcon 设置为 null 时,它仍然会将搜索文本推到相当多的位置,就好像图标仍然在那里占用空间一样。这个答案可以消除那个空白
              【解决方案11】:

              你可以试试这个:

                public class MainActivity extends Activity {
              
                      @SuppressLint("NewApi")
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
              
                          RelativeLayout relative = new RelativeLayout(this);
                          LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
                          relative.setLayoutParams(params);
                          setContentView(relative);
              
                          SearchView searchView = new SearchView(this);
              searchView.setIconifiedByDefault(false);
                          traverseView(searchView, 0);
                  //      searchView.setIconifiedByDefault(false);
                          LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                  //      searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                          searchView.setLayoutParams(searchViewparams);
                          relative.addView(searchView);
                      }
                      @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                      @SuppressLint("NewApi")
                      private void traverseView(View view, int index) {
                          if (view instanceof SearchView) {
                              SearchView v = (SearchView) view;
                              for(int i = 0; i < v.getChildCount(); i++) {
                                  traverseView(v.getChildAt(i), i);
                              }
                          } else if (view instanceof LinearLayout) {
                              LinearLayout ll = (LinearLayout) view;
                              for(int i = 0; i < ll.getChildCount(); i++) {
                                  traverseView(ll.getChildAt(i), i);
                              }
                          } else if (view instanceof EditText) {
                              ((EditText) view).setTextColor(Color.GREEN);
                              ((EditText) view).setHintTextColor(Color.BLACK); 
                          } else if (view instanceof TextView) {
                              ((TextView) view).setTextColor(Color.BLUE);
                          } else if (view instanceof ImageView) {
                              ((ImageView) view).setImageResource(R.drawable.ic_launcher);
                          } else {
                              Log.v("View Scout", "Undefined view type here...");
                          }
                      }
                  }
              

              【讨论】:

              • 对不起,哥们,我没有告诉你上面是我写的整个代码,请发表评论,并在代码中添加上面的行
              • yaar just Seticonifiedbydefault karee toh bhi yehi procedure aa jayega 但我只想移除那个放大镜,我的意思是我不应该不惜一切代价得到那个提示
              • 是的,Seticonifiedbydefault 删除您需要的默认图标。
              【解决方案12】:

              对于 v7 Widget,如果您使用 searchView.setIconifiedByDefault(false),搜索视图的行为会有所不同。您只需在 xml 中将提示图标设置为 @null。您也可以将这种方式应用于其他图标。

              【讨论】:

                【解决方案13】:

                androidx.appcompat.widget.SearchView

                如果你使用 androidx.appcompat.widget.SearchView:

                您可以使用以下方法隐藏图标:

                val magImage = searchView.findViewById<View>(androidx.appcompat.R.id.search_mag_icon) as ImageView
                magImage.layoutParams = LinearLayout.LayoutParams(0, 0)
                

                【讨论】:

                  【解决方案14】:

                  我知道它很丑,但它只对我有用:

                  MenuItem menuSearch = menu.findItem(R.id.menu_search);
                  mSearchView = (SearchView) menuSearch.getActionView();
                  
                  SearchView.SearchAutoComplete searchAutoComplete = mSearchView.findViewById(androidx.appcompat.R.id.search_src_text);
                  
                  Class<?> clazz = Class.forName("androidx.appcompat.widget.SearchView$SearchAutoComplete");
                  SpannableStringBuilder stopHint = new SpannableStringBuilder("");
                  stopHint.append(getString(R.string.search_hint));
                  Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
                  setHintMethod.invoke(searchAutoComplete, stopHint);
                  

                  【讨论】:

                    【解决方案15】:

                    这是一个老问题,但这对我来说帮助很大:如果你想隐藏在菜单中,这里发布的问题都对我没有用。解决问题的是: 而不是放

                    app:actionViewClass="android.widget.SearchView"
                    

                    在菜单项内,您可以更改为:

                    app:actionLayout="@layout/search_layout"
                    

                    而且,在这个布局中,你可以添加这段代码:

                    <?xml version="1.0" encoding="utf-8"?>
                    <android.widget.SearchView
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/search_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:searchHintIcon="@drawable/nothing_drawed"/>
                    

                    最后,在可绘制对象中创建它,里面什么都没有,例如:

                    <?xml version="1.0" encoding="utf-8"?>
                    <selector xmlns:android="http://schemas.android.com/apk/res/android">
                    
                    </selector>
                    

                    只有这种方法对我有用。当我试图隐藏 searchHintIcon,设置为 @null 或其他任何东西时,它仍然和我没有编写任何代码一样。

                    【讨论】:

                      【解决方案16】:

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

                      app:searchHintIcon="@null" />

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2013-12-04
                        • 1970-01-01
                        • 2017-05-18
                        相关资源
                        最近更新 更多