简体   繁体   中英

SearchView not showing queryHint text

I have a search view with a background drawable, but I can't seem to get the text to appear no matter what I try. No text shows up in the SearchView.

I've tried the android:text and android:queryHint and even setting it programmatically using mSearchView.setQueryHintText("search for something"); but none of these work. The searchView is not inside an actionBar.

Here is the xml for the searchview

<android.support.v7.widget.SearchView
        android:id="@+id/m_search_view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/searchbar_height"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toLeftOf="@+id/other_button"
        android:background="@drawable/rounded_corner_box"
        android:focusableInTouchMode="true"
        android:gravity="center_vertical|left"
        android:paddingLeft="@dimen/left_padding"
        android:queryHint="search for something"
        android:textAppearance="@style/m_text_appearance"
        android:textColor="@color/m_text_color" />

When I don't set a background, I don't see the searchView at all (the searchView is translucent).

Any help would be appreciated.

searchView.setIconified(false);

在 XML 中,如果您已经为android:queryHint设置了值,请添加android:iconifiedByDefault="false"

For android.support.v7.widget.SearchView, instead of using

android:queryHint="your hint" 
android:iconifiedByDefault="false"

you must use

app:queryHint="your hint" 
app:iconifiedByDefault="false"

In the xml layout file just add

app:iconifiedByDefault="false"

Notice that it's app: and not android:

Fixed my own issue: I had to dig into the SearchView's source. This is how I got the background and text to show in the searchView:

SearchView searchView = (SearchView)mLayout.findViewById(R.id.m_searchview);
searchView.setQueryHint("search for something");
View searchTextView = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchTextView.setBackground(
ResourcesCompat.getDrawable(getResources(), R.drawable. rounded_corner_box, getTheme()));
View searchFrame = searchView.findViewById(android.support.v7.appcompat.R.id.search_edit_frame);
searchFrame.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.m_background, getTheme()));

在xml布局文件中添加

app:queryHint="My hint"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM