Android - 不能更改 ActionBar 中搜索图标的颜色吗?

Android - Is this not possible to change the color of search icon in ActionBar?

我在 android 项目中使用默认值 ActionBar。我正在使用 Theme.AppCompat.Light.DarkActionBar 主题。

我已经为 menu 创建了 XML 文件,menusearch item 如下:

<item
    android:id="@+id/search_button"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Search"
    app:showAsAction="always" />

当我在物理设备上加载应用程序以测试 search icon 的颜色不是白色时,它有点偏红(ActionBar 的低颜色),请参见下图:

标题颜色为白色没问题,溢出菜单按钮颜色为白色没问题,但搜索图标按钮不是白色。因此,我进行了研究,发现了以下无人使用的来源。

  1. Change the text colors and icons of searchview in action bar.
  2. And all these .....

最终,我意识到无法更改 ActionBar 中默认搜索图标的颜色。我只想让这个问题成为这个问题的最后一个长篇解释,涵盖 Why the icon color is different then the title color and overflow menu icon color?Why this thing is impossible to solve?Where this color property of xml for search icon is hidden that is hard to find?How(if possible) to change the color of this default search icon in the ActionBar?我知道我们可以在这里添加白色的自定义图标,我们也有 ToolBar,但我不想要这个解决方案,因为如果可以更改 ActionBar 中的标题,请添加溢出菜单ActionBar(等)中的选项,那么也应该可以更改 ActionBar 中搜索图标的颜色。可能是我找到了一个隐藏的极客,他知道这个问题并且可以解释 WhyWhat 使这个问题成为未来 android 人的最后一个 informative/solution。

如果有人想否定我的问题,我很乐意接受这个问题,但至少这个问题对以后的 android 新手有帮助。谢谢!!!

Why the icon color is different then the title color and overflow menu icon color?

@android:drawable/ic_menu_search(你可以在Android\Sdk\platforms\android-28\data\res\drawable中找到它们)是一个带有一些透明白色的png文件(这就是你得到白色偏红色的原因)

Why this thing is impossible to solve?

这并非不可能,您可以创建自己的图标,如下面的答案所示。

Where this color property of xml for search icon is hidden that is hard to find?

没有xml。只是png文件。根据主题 android 将选择其中之一。(即对于 Light Action 栏为黑色图标,对于 Dark Action 栏为白色图标)

How(if possible) to change the color of this default search icon in the ActionBar?

解决方案是为搜索图标创建自己的矢量图像。您可以指定任何您想要的颜色。然后在菜单中使用这个图标。

第 1 步:创建矢量图像

右键单击 drawable 文件夹 -> 新建 -> Vector Assest

第 2 步:创建搜索图标并选择颜色

更改 Clipartcolor

第 3 步:在菜单中使用此图标

<item
    android:id="@+id/search_button"
    android:icon="@drawable/ic_search_black_24dp"
    android:title="Search"
    app:showAsAction="always" />

使用下面的矢量图

ic_search.xml (res/drawable/ic_search.xml)

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="#FFFFFF"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z" />
</vector>