如何更改SearchView的样式?

时间:2019-05-21 13:43:33

标签: java android searchview

我必须在应用程序中实现SearchView

XML声明如下:

<SearchView
     android:id="@+id/searchView"
     style="@style/SearchViewStyle"
     android:layout_width="wrap_content"
     android:layout_height="20dp"
     android:layout_alignParentEnd="true"
     android:layout_marginTop="5dp"
     android:layout_marginBottom="5dp"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintTop_toTopOf="parent" />

样式是:

<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
 <item name="android:searchIcon">@drawable/ico_search</item>
 <item name="android:queryBackground">@android:color/transparent</item>

最后是活动:

SearchView mSearchView = findViewById(R.id.searchView);
mSearchView.setIconifiedByDefault(true);
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint("Search here");

我不知道如何在输入用于研究的关键字时更改提示的文本颜色和“ edittext”的背景。

我刚刚尝试了什么?

在style.xml中添加此行

<item name="android:textSize">18sp</item>
<item name="android:textColor">@color/white</item>

或将其添加到我的活动中

AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(color.black));
searchText.setTextColor(getResources().getColor(color.black));

这些解决方案不起作用。

2 个答案:

答案 0 :(得分:0)

您可以通过使用shape使用具有相同属性的searchBox的EditeText:

  <EditText
                      android:id="@+id/edt_search"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:background="@drawable/shape_rectangle_edittext"
                      android:drawableRight="@drawable/ic_search_gray"
                      android:ems="10"
                      android:hint="@string/search_box"
                      android:imeOptions="actionSearch"
                      android:inputType="text"
                      android:padding="@dimen/size10"
                      android:textColorHint="#444"
                      android:textSize="@dimen/size16"
                      />
可绘制的

和shape_rectangle_edittext.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" android:padding="10dp">
    <solid android:color="#777"/>
    <corners
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"/>
</shape>

答案 1 :(得分:0)

您可以这样设置SearchView的样式:

<style name="Theme.MyTheme" parent="Theme.AppCompat">
   <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
   <!-- Background for the search query section (e.g. EditText) -->
   <item name="queryBackground">...</item>
   <!-- Background for the actions section (e.g. voice, submit) -->
   <item name="submitBackground">...</item>
   <!-- Close button icon -->
   <item name="closeIcon">...</item>
   <!-- Search button icon -->
   <item name="searchIcon">...</item>
   <!-- Go/commit button icon -->
   <item name="goIcon">...</item>
   <!-- Voice search button icon -->
   <item name="voiceIcon">...</item>
   <!-- Commit icon shown in the query suggestion row -->
   <item name="commitIcon">...</item>
   <!-- Layout for query suggestion rows -->
   <item name="suggestionRowLayout">...</item>
</style>

以下是详细信息https://philio.me/styling-the-searchview-with-appcompat-v21/