AppCompat SearchView icon can't be GONE

给你一囗甜甜゛ 提交于 2020-01-14 04:48:46

问题


I'm using android.support.v7.widget.SearchView and i need to make this search icon GONE. I have custom ActionBar layout with SearchView:

        <android.support.v7.widget.SearchView
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

initialize SearchView :

    searchView = (SearchView) findViewById(R.id.search);

    AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);
    searchText.setPadding(0, 0, 0, -padding);
    searchText.setHintTextColor(getResources().getColor(R.color.light_gray));

    View searchPlate = searchView.findViewById(R.id.search_plate);
    searchPlate.setBackgroundResource(R.drawable.searchview_textfield);

    View close = searchPlate.findViewById(R.id.search_close_btn);
    close.setBackgroundResource(R.drawable.selectable_background_orange);

I was trying to customize searchIcon like this:

ImageView icon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

trying to set everything from here Android SearchView Icon but it's not that ImageView. Can anyone help me plz?


回答1:


Hey Sorry I'm a little late to the party. I am having to do a similar thing. I need to change the icon. It's a bit of a hack but I think its the best possible solution. Looking in the support library code AOSP uses "Search_mag_icon" as the icon you are looking for. In your code you can do something like: int searchMagIcon = getResources().getIdentifier("search_mag_icon", "id", "android"); ImageView SearchHintIcon = (ImageView) searchView.findViewById(searchMagIcon); and that will save that ImageView. Then you can simply change it or set it to null in your case.

Edit There is also a mSearch.getSearchView().setIconifiedByDefault() as well as a mSearch.getSearchView().setIconified() that you can use. I find that you MUST set the bydefault one to see results.

Hope that helps. :-)




回答2:


public static void setSearchViewEditTextHint(Context context, SearchView searchView, int stringId){
    int searchMagIconId = context.getResources().getIdentifier("android:id/search_src_text", null, null);
    TextView textView = (TextView) searchView.findViewById(searchMagIconId);
    if (null != textView) {
        textView.setHint(stringId);
    }
}

Function call:

setSearchViewEditTextHint(this, searchView, R.string.emptyString);


来源:https://stackoverflow.com/questions/21095523/appcompat-searchview-icon-cant-be-gone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!