我有一个背景可绘制的搜索视图,但无论我尝试什么,我似乎都无法显示文本。 SearchView中没有文字显示。
我尝试了android:text
和android:queryHint
,甚至使用mSearchView.setQueryHintText("search for something");
以编程方式设置它,但这些都不起作用。 searchView不在actionBar中。
以下是searchview的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是半透明的)。
任何帮助都将不胜感激。
答案 0 :(得分:6)
searchView.setIconified(false);
答案 1 :(得分:3)
修正了我自己的问题:我不得不深入研究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()));
答案 2 :(得分:2)
如果您已为android:iconifiedByDefault="false"
设置了值,请在XML中添加android:queryHint
。
答案 3 :(得分:1)
在xml布局文件中添加
app:queryHint="My hint"
答案 4 :(得分:0)
对于android.support.v7.widget.SearchView,而不是使用
android:queryHint="your hint"
android:iconifiedByDefault="false"
您必须使用
app:queryHint="your hint"
app:iconifiedByDefault="false"
答案 5 :(得分:0)
只需在xml布局文件中添加
app:iconifiedByDefault="false"
请注意,它是 app: ,而不是 android: