删除搜索字段中的放大镜图标

时间:2014-09-17 15:52:34

标签: android user-interface

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

Magnifier icon of android edittext search view

解决方案====

它是一个仅在提示文本中设置的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

1 个答案:

答案 0 :(得分:0)

我设法在我的应用程序中这样做(更改图标,但你可以调整它来隐藏它)。如果某人有一个更简单的方法,我会很高兴听到它(例如在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