android.support.v7.widget.SearchView setQueryHint?

时间:2016-10-21 10:11:22

标签: java android

我想为android.support.v7.widget.SearchView添加一个提示,找到引用android.widget.SearchView的{​​{3}},如何使用android.support.v7.widget.SearchView设置查询提示?

修改

澄清一下,虽然android.support.v7.widget.SearchView应该有setQueryHint,但事实并非如此。

以下是我所有依赖项的列表:

compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.facebook.android:facebook-android-sdk:4.15.0'
compile 'com.parse:parsefacebookutils-v4-android:1.10.4@aar'
compile 'com.parse:parse-android:1.13.1'
compile 'com.parse:parsetwitterutils-android:1.10.5'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:0.4.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'uk.co.chrisjenx:calligraphy:2.2.0'

minSdk为19,targetSdk为24

一些代码段:

在顶部:import android.support.v7.widget.SearchView;

代码:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.menu_search, menu);

    MenuItem searchItem = menu.findItem(R.id.search);

    if (searchItem != null) {
        searchView = (SearchView) searchItem.getActionView();
    }
    if (searchView != null) {
        searchView.setIconifiedByDefault(false);
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchItem.setQueryHint("Query Hint");
    }

    super.onCreateOptionsMenu(menu, inflater);
}

R.menu.menu_search

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/search"
        android:title="@string/hint_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />
</menu>

1 个答案:

答案 0 :(得分:2)