【问题标题】:Remove magnifier icon in searchfield删除搜索字段中的放大镜图标
【发布时间】:2014-09-17 15:52:33
【问题描述】:

如何在 Android SearchView 中更改或删除 EditText 输入的放大镜图标?

解决方案 ====

它是一个仅在提示文本中设置的 ImageSpan。

简单地做:

int id = resources.getIdentifier("search_src_text", "id", "android");
View autoComplete = searchView.findViewById(id);
autoComplete.setHint(R.string.what_you_like) // or a custom span

【问题讨论】:

标签: android user-interface


【解决方案1】:

我设法在我的应用程序中做到了这一点(更改图标,但您可以调整它以隐藏它)。如果有人有更简单的方法,我会很高兴听到它(例如在 XML 中)。

    try {
        Resources resources = this.getResources();
        int id = resources.getIdentifier("search_src_text", "id", "android");
        View autoComplete = searchView.findViewById(id);
        Class<?> clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
        Method textSizeMethod = clazz.getMethod("getTextSize");
        Float rawTextSize = (Float) textSizeMethod.invoke(autoComplete);
        int textSize = (int) (rawTextSize * 1.25);

        Drawable searchIcon = resources.getDrawable(R.drawable.ic_action_search);
        searchIcon.setBounds(0, 0, textSize, textSize);

        SpannableStringBuilder stopHint = new SpannableStringBuilder("   ");
        stopHint.append(this.getString(R.string.search_quotes));
        stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
        setHintMethod.invoke(autoComplete, stopHint);
    }
    catch (Exception exception) {
        exception.printStackTace();
    }

来自How to style the ActionBar SearchView programmatically

【讨论】:

  • 您应该感谢原始开发者:nlopez.io/…
  • 我找不到原始链接...谢谢提供
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多