如何更改SearchView元素的颜色?

25

我想在我的项目中使用SearchView,但是元素的颜色出现了问题。让我给你看看:这是我的SearchView所在的fragmentDialog

你可以在这里看到我的意思

我不需要改变背景颜色。下一张图片仅为示例。我想看到没有黑色背景的SearchView元素。

元素的颜色是白色

我已经尝试过:

  • 更改主题
  • 更改样式
  • 更改色调

但是什么都没用。搜索图标和其他元素仍然是白色的。也许我漏掉了什么?我能在XML中做到这一点吗?请帮帮我。


你想要动态设置图标和背景颜色吗? - Bhavnik
不,我不想这样做。如果可能的话,我想将其设置在XML中并且不再进行修改。 - Alex Smith
9个回答

49
在这个答案中,我假设 SearchView 已经添加到了 Toolbar 中,并且 AppThemeTheme.AppCompat.Light.NoActionBar 的子项。
<style name="AppTheme.Toolbar" parent="AppTheme">
    <!--This line changes the color of text in Toolbar-->
    <item name="android:textColorPrimary">@color/green</item>
    <!--This line changes the color of icons in toolbar (back, overflow menu icons)-->
    <item name="android:textColorSecondary">@color/green</item>
</style>
现在将 AppTheme.Toolbar 作为您的工具栏主题。
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:theme="@style/AppTheme.Toolbar" />

但是如果我在工具栏之外使用SearchView,它会起作用吗? - Alex Smith
如果SearchView是在Toolbar中显示的菜单项,它将起作用。 - rahulrvp
谢谢兄弟。但我尝试了编程,但没有成功。 - Deepak Borade
1
经过漫长的研究,这应该是被接受的答案!感谢您提供的解决方案! - Teo
离题了,这个问题不是关于在工具栏中搜索的。 - user924

24

你需要使用android.support.v7.widget.SearchView

尝试在你的searchview中使用以下样式,在扩展搜索视图之前:

图片描述

在扩展搜索视图之后 --->

图片描述

<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/search_view"
            style="@style/SearchViewStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_gravity="end" />

<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
    <!-- Gets rid of the search icon -->
    <item name="searchIcon">@drawable/ic_search</item>
    <!-- Gets rid of the "underline" in the text -->
    <item name="queryBackground">@color/white</item>
    <!-- Gets rid of the search icon when the SearchView is expanded -->
    <item name="searchHintIcon">@null</item>
    <!-- The hint text that appears when the user has not typed anything -->
    <item name="queryHint">@string/search_hint</item>
</style>

1
我想你没有使用android.support.v7.widget.SearchView。 - Bhavnik
是的,你说得对。我在你回答之前2个小时就找到了它 :D https://dev59.com/5FkR5IYBdhLWcg3w1gVt#40791256 - Alex Smith
1
文字颜色最终变成了白色。 - Johann
你不要在这里更改任何颜色,离题了。 - user924
@user924,您想要做什么?请告诉我。在我看来,肯定是更改背景颜色,因为我已经测试过了。 - Bhavnik
@Bhavnik 这里是正确的答案 https://dev59.com/5FkR5IYBdhLWcg3w1gVt#54273767 - user924

16
您可以在`styles.xml`中使用自定义风格来修改搜索视图:
<style name="AppSearchView" parent="Widget.AppCompat.SearchView" >
    <!-- Text Size -->
    <item name="android:textSize">@dimen/textSizePrimary</item>
    <!-- Query Text Color -->
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <!-- Hint Color -->
    <item name="android:textColorHint">@color/textColorDisabled</item>
    <!-- Icon Color -->
    <item name="android:tint">@color/textColorPrimary</item>
</style>

然后在您的搜索视图中使用此样式:

<android.support.v7.widget.SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
    app:theme="@style/AppSearchView" />

如果被接受,我们只需要一个答案来展示如何改变颜色。parent="Widget.AppCompat.SearchView"非常重要,以使其正常工作。在<androidx.appcompat.widget.SearchView ... />中也可以正常工作。 - user924

6

Searchview 是由支持库提供的一种具有更改图标功能的搜索视图。

<androidx.appcompat.widget.SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:searchIcon="@drawable/ic_search"
            app:closeIcon="@drawable/ic_close_black"
            app:commitIcon=""
            app:goIcon=""
            app:searchHintIcon=""
            app:voiceIcon=""
    >

5
int searchiconId = view.getContext().getResources().getIdentifier("android:id/search_button",null,null);
ImageView imgView = (ImageView)findViewById(searchiconId);
Drawable whiteIcon = img.getDrawable();
whiteIcon.setTint(Color.RED); //Whatever color you want it to be
img.setImageDrawable(whiteIcon);

5

尝试将以下行添加到样式中。

android:textColorPrimary 改变用户在 EditText 中输入的文本颜色。 android:textColorSecondary 改变后退箭头、“删除文本”叉和提示前面的小搜索图标。

<item name="android:textColorPrimary">@color/white</item>
<item name="android:textColorSecondary">@color/white</item>

1
所以,我的问题是什么。我使用了android.widget.SearchView而不是android.support.v7.widget.SearchView。我更改了SearchView,之后提出的解决方案开始工作。如果有人能解释为什么样式和其他不同的东西不起作用,或者可以给我一个链接,我将不胜感激。

1

如果您想更改搜索视图中放大镜的颜色,这将对您有所帮助。如果您使用的是Material主题,则可以执行以下操作:

 ImageView icon = album_search.findViewById(com.google.android.material.R.id.search_button);
        Drawable whiteIcon = icon.getDrawable();
        whiteIcon.setTint(Color.BLACK); //Whatever color you want it to be
        icon.setImageDrawable(whiteIcon);

如果使用Appcompact主题,只需将com.google.android.material.R.id.search_button更改为android.support.v7.appcompat.R.id.search_button即可。
就是这样。

0
在这个答案中,我改变了搜索图标、搜索提示图标、关闭图标、板块、查询提示颜色和查询颜色的颜色。
view!!.findViewById<EditText>(
            searchView.context.resources.getIdentifier(
                "android:id/search_src_text",
                null,
                null
            )
        ).apply {
            setTextColor(ContextCompat.getColor(context!!, R.color.darkThemeIconColor))
            setHintTextColor(ContextCompat.getColor(context!!, R.color.colorAccent))
        }

        view!!.findViewById<ImageView>(
            searchView.context.resources.getIdentifier(
                "android:id/search_button",
                null, null
            )
        ).imageTintList = ColorStateList.valueOf(
            ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
        )
        val mDrawable = SearchView::class.java.getDeclaredField("mSearchHintIcon")
        mDrawable.isAccessible = true
        val drawable = mDrawable.get(searchView) as Drawable
        drawable.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
            ContextCompat.getColor(context!!, R.color.darkThemeIconColor),
            BlendModeCompat.SRC_ATOP
        )
        view!!.findViewById<ImageView>(
            searchView.context.resources.getIdentifier(
                "android:id/search_close_btn",
                null, null
            )
        ).imageTintList = ColorStateList.valueOf(
            ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
        )
        view!!.findViewById<View>(
            searchView.context.resources.getIdentifier(
                "android:id/search_plate",
                null, null
            )
        ).backgroundTintList = ColorStateList.valueOf(
            ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
        )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接