You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改SearchView中searchHintIcon的颜色(非搜索状态颜色)

How to Change the Color of SearchView's searchHintIcon (Hint State Only)

Got it, let's break down exactly how to tweak the color of that search hint icon—the one that shows up when your SearchView is collapsed and displaying the hint text, not the active search state. Here are a couple of reliable methods depending on whether you prefer XML or dynamic code changes:

Method 1: Using XML Styles (Static Color)

This is great if you want a fixed color for all instances of your SearchView.

First, create a custom style in your res/values/styles.xml file, inheriting from the AppCompat SearchView style (for better cross-version compatibility):

<style name="CustomSearchView" parent="Widget.AppCompat.SearchView">
    <!-- Option 1: Tint the default hint icon directly -->
    <item name="searchHintIcon">@drawable/ic_search</item> <!-- Use the default search icon or your own -->
    <item name="tint">@color/your_target_color</item> <!-- Applies tint to the hint icon; note this affects other icons too -->

    <!-- Option 2: Use a pre-tinted drawable for precise control -->
    <!-- If you don't want to tint all icons, create a drawable with your desired color and reference it here -->
    <!-- <item name="searchHintIcon">@drawable/ic_search_tinted</item> -->
</style>

Then apply this style to your SearchView in the layout:

<androidx.appcompat.widget.SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/CustomSearchView" />

Method 2: Dynamic Color Change via Code

Perfect if you need to adjust the color at runtime (e.g., based on theme changes or user preferences).

Kotlin

val searchView = findViewById<SearchView>(R.id.searchView)
// Locate the hint icon view (AppCompat SearchView uses this specific ID)
val hintIcon = searchView.findViewById<ImageView>(androidx.appcompat.R.id.search_mag_icon)
hintIcon?.setColorFilter(
    ContextCompat.getColor(this, R.color.your_target_color),
    PorterDuff.Mode.SRC_IN
)

Java

SearchView searchView = findViewById(R.id.searchView);
ImageView hintIcon = searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
if (hintIcon != null) {
    hintIcon.setColorFilter(
        ContextCompat.getColor(this, R.color.your_target_color),
        PorterDuff.Mode.SRC_IN
    );
}

Quick Notes

  • Stick with androidx.appcompat.widget.SearchView instead of the native android.widget.SearchView—the hint icon ID differs between the two, and AppCompat offers smoother compatibility across Android versions.
  • If you must use the native SearchView, replace androidx.appcompat.R.id.search_mag_icon with android.R.id.search_button.
  • The SRC_IN PorterDuff mode ensures the color applies correctly without breaking the icon's transparency.

内容的提问来源于stack exchange,提问作者Ritu Suman Mohanty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:03:59