Remove the SearchIcon as hint in the searchView

后端 未结 13 2246
攒了一身酷
攒了一身酷 2020-12-03 09:59

I am doing an application where for example when i click on the image(it is a searchView)

\"enter

13条回答
  •  无人及你
    2020-12-03 10:29

    I just met the same problem, here are some reference links.

    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

    The key is in the following function, it sets the icon as an 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;
    }
    

提交回复
热议问题