问题
I want to to create a searchview which should have a search icon and hint (as in EditText) . The default searchview in android has only the search icon . It does not have hint like edittext.
I am able to add hint to searchview but it is only visible after clicking on the search icon on the search-view . I am using the following code to add hint to the search-view .
String locationSearchViewSetQueryHint = "<font color = #ffffff>Location</font>"
locationSearchView.setQueryHint(Html.fromHtml(locationSearchViewSetQueryHint))
How can I show the hint with search icon?
回答1:
Use this
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:padding="5dp"
android:background="@drawable/edit_text_bg"
android:gravity="left"
android:hint="Search"
android:drawableStart="@drawable/search_icon">
回答2:
It worked for me give a try,
<SearchView
android:id="@+id/sv_full_list_symbols"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:gravity="center"
android:queryHint="@string/R471"
android:textColor="@color/white"
android:textSize="14sp" >
</SearchView>
Add this in your code,
fullSearchView = (SearchView) findViewById(R.id.sv_full_list_symbols);
SearchManager searchManager1 = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
fullSearchView.setSearchableInfo(searchManager1.getSearchableInfo(getComponentName()));
fullSearchView.setIconifiedByDefault(false);
Happy Coding...
来源:https://stackoverflow.com/questions/32064877/searchbox-with-hint-and-search-icon-on-left