Android support library SearchView xml attributes queryHint and iconifiedByDefault not working

北城余情 提交于 2019-11-28 18:52:06
petrnohejl

You are missing required namespace. Replace [yourapp] with your app name in XML layout. Use this namespace for SearchView properties. This approach is necessary if you use views from support library. Btw. you must do the same with menu for AppCompat action bar.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:[yourapp]="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.SearchView
        android:id="@+id/fragment_address_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/global_bg_front"
        android:textSize="@dimen/global_text_medium"
        android:textColor="@color/global_text_primary"
        [yourapp]:queryHint="@string/fragment_address_search_hint"
        [yourapp]:iconifiedByDefault="false" />

</LinearLayout>

You can also setup your SearchView programmatically. See my template on GitHub.

private void setupSearchView(SearchView searchView)
{
    // search hint
    searchView.setQueryHint(getString(R.string.fragment_address_search_hint));

    // background
    View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    searchPlate.setBackgroundResource(R.drawable.searchview_bg);

    // icon
    ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    searchIcon.setImageResource(R.drawable.searchview_icon);

    // clear button
    ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setImageResource(R.drawable.searchview_clear);

    // text color
    AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchText.setTextColor(getResources().getColor(R.color.global_text_primary));
    searchText.setHintTextColor(getResources().getColor(R.color.global_text_secondary));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!