Android支持库SearchView xml属性queryHint和iconifiedByDefault无法正常工作

for*_*ill 15 android searchview

我在布局文件中有以下SearchView

<android.support.v7.widget.SearchView
    android:id="@+id/search_view"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:queryHint="@string/search"
    android:iconifiedByDefault="false"

     />
Run Code Online (Sandbox Code Playgroud)

但是,当我运行我的应用程序时,没有提示并且searchview被图标化,在代码中它可以工作我的问题是这是一个支持库中的错误吗?

pet*_*ejl 41

您缺少必需的命名空间.[yourapp]在XML布局中替换为您的应用名称.将此命名空间用于SearchView属性.如果您使用支持库中的视图,则必须使用此方法.顺便说一句.您必须对AppCompat操作栏的菜单执行相同操作.

<?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>
Run Code Online (Sandbox Code Playgroud)

您还可以通过编程方式设置SearchView.在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));
}
Run Code Online (Sandbox Code Playgroud)