SearchView 不显示查询提示文本

SearchView not showing queryHint text

我有一个带有可绘制背景的搜索视图,但无论我尝试什么,我似乎都无法显示文本。 SearchView 中没有显示任何文本。

我已经尝试了 android:textandroid:queryHint,甚至使用 mSearchView.setQueryHintText("search for something"); 以编程方式设置它,但 none 这些工作。 searchView 不在 actionBar 内。

这是搜索视图xml

<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" />

当我不设置背景时,我根本看不到searchView(searchView是半透明的)。

如有任何帮助,我们将不胜感激。

解决了我自己的问题:我不得不深入研究 SearchView 的源代码。这就是我如何在 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()));
searchView.setIconified(false);

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

在 xml 布局文件中添加

app:queryHint="My hint"

对于android.support.v7.widget.SearchView,而不是使用

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

你必须使用

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

在 xml 布局文件中只需添加

app:iconifiedByDefault="false"

请注意,它是 app: 而不是 android: