【问题标题】:Android support library SearchView xml attributes queryHint and iconifiedByDefault not workingAndroid 支持库 SearchView xml 属性 queryHint 和 iconifiedByDefault 不起作用
【发布时间】:2013-11-09 21:45:45
【问题描述】:

我在布局文件中有以下 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"

     />

但是,当我运行我的应用程序时,没有出现任何提示并且搜索视图被图标化,在代码中它可以工作我的问题是这是支持库上的错误吗?

【问题讨论】:

    标签: android searchview


    【解决方案1】:

    您缺少必需的命名空间。在 XML 布局中将 [yourapp] 替换为您的应用名称。将此命名空间用于 SearchView 属性。如果您使用支持库中的视图,则此方法是必要的。顺便提一句。您必须对 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>
    

    您还可以通过编程方式设置 SearchView。见我的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));
    }
    

    【讨论】:

    • 谢谢,我知道 AppCompact 是如何工作的,但不知道支持库也是如此
    • 你能帮我吗,如何让 searchIcon GONE 并且在提示之前不要使用空格?
    • AndrewS:尝试使用 AutoCompleteTextView 而不是 SearchView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多