Android expanded SearchView makes other ActionBar items disappear

后端 未结 2 1750
生来不讨喜
生来不讨喜 2020-12-18 08:21

I\'m facing some problems with a SearchView in a Contextual Action Bar called by a Fragment. The major one is that when my SearchView is expanded, it makes disappear all oth

2条回答
  •  情书的邮戳
    2020-12-18 08:46

    I'm completing this answer thanks to link posted by Bronx in his last post and other answer like this Android - cannot find TextView inside SearchWidget when using Android Support library (accepted answer): to move magnify icon inside text view, I remove this line from first Bronx answer

    searchView.setIconifiedByDefault(false);
    

    Then I've pasted this just abfter Bronx's code:

        AutoCompleteTextView autoComplete = (AutoCompleteTextView)mSearchView.findViewById(R.id.search_src_text);
        Class clazz = null;
        try {
            clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
            SpannableStringBuilder stopHint = new SpannableStringBuilder("  ");
            stopHint.append(getString(R.string.action_search));
            // Add the icon as an spannable
            Drawable searchIcon = getResources().getDrawable(R.drawable.abc_ic_search_api_mtrl_alpha);
            Method textSizeMethod = clazz.getMethod("getTextSize");
            Float rawTextSize = (Float)textSizeMethod.invoke(autoComplete);
            int textSize = (int) (rawTextSize * 1.25);
            searchIcon.setBounds(0, 0, textSize, textSize);
            stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // Set the new hint text
            Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
            setHintMethod.invoke(autoComplete, stopHint);
        } catch (Exception e) {
            // Set default hint
            mSearchView.setQueryHint(getResources().getString(R.string.action_search));
            e.printStackTrace();
        }
    

    Just pay attention that R.string.action_search refers to a custom string in my project, so change it with yours. Thanks again to Bronx.

提交回复
热议问题