如何将工具栏 SearchView 下划线颜色更改为灰色

How to change the toolbar SearchView Underline color to GRAY

我试图在展开搜索栏时更改搜索视图的下划线颜色`

    SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
    autoCompleteTextView.setCursorVisible(true);
    autoCompleteTextView.setTextColor(ContextCompat.getColor(this, R.color.textcolor_light_gray));
    autoCompleteTextView.setHintTextColor(autoCompleteTextView.getTextColors().withAlpha(255));
    autoCompleteTextView.setTextSize(16);

    View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    searchplate.setBackgroundResource(R.drawable.autocomplete_bg);

这里我也用主题改了颜色

 <style name="MySearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
    <!-- Background for the search query section (e.g. EditText) -->
    <item name="queryBackground">@color/darkgray</item>

</style>

但对我没有任何用处。任何人都可以帮助我...

终于找到解决办法了..

    View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    searchplate.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);

您也可以通过xml更改它:

<item name="android:queryBackground">...</item>

我猜你忘了 android: 前缀

下面的示例对我有用,希望对你也有用。

<SearchView
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:iconifiedByDefault="false"
    android:gravity="center"
    android:background="@drawable/bg_searchview"
    android:queryBackground="@drawable/bg_searchview_query"/>

android:queryBackground="@drawable/bg_searchview_query"

在 drawable 中,为背景创建 .xml 文件 bg_searchview

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android">
    <padding
        android:top="10dp"
        android:bottom="10dp" />
    <solid android:color="#FFFFFF" />
    <corners android:radius="20dp"/>
    <stroke
        android:width="0.3dp"
        android:color="#797979" />
</shape>

在 drawable 中,为 queryBackground

创建一个 .xml 文件 bg_searchview_query
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <corners android:radius="20dp"/>
</shape>