在Android工具栏中更改SearchView返回和清除图标的颜色

8
我希望您可以在SearchView折叠时自定义返回和清除图标。我的要求是将颜色更改为白色,但现在是黑色的(请参见下面分享的图片)。我试图解决这个问题已经几天了,但我尝试过的解决方案都没有奏效。我尝试了SO提供的以下一些解决方案: 解决方案1:
<style name="AppTheme" parent="@style/Theme.Base.AppCompat.Light.DarkActionBar">
    <item name="actionBarWidgetTheme">@style/YourActionBarWidget</item>
</style>

<style name="YourActionBarWidget" parent="@style/Theme.AppCompat">
    <item name="searchViewCloseIcon">@drawable/xxx</item>
</style>

解决方案2:

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.WHITE), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);  

我的项目 styles.xml 文件。
<style name="ToolBarTheme" parent="@style/Theme.AppCompat.Light">
        <item name="colorPrimary">@color/blue</item>
        <item name="colorPrimaryDark">@color/blue</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>
        <item name="android:actionMenuTextColor">@color/white</item>
        <item name="actionMenuTextColor">@color/white</item>

    </style>



<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@color/white</item>
        <item name="gapBetweenBars">2.5dip</item>
        <item name="thickness">2dp</item>
    </style>

我的问题截图:

enter image description here

请帮助我解决这个问题,任何解决方案和建议对我都非常有帮助。感谢您的支持。


你尝试过覆盖drawerToggle吗? - Ramesh
@Ramesh 谢谢您的回复。请查看我的更新答案。 - Chandru
你的主题为什么要继承Theme.AppCompat.Light? - Ramesh
3个回答

8
如果你正在寻找工具栏上的白色图标,请将以下代码添加到工具栏标签中。
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

3

我假设你将这个作为你的基础应用程序主题。

<style name="AppTheme" parent="@style/Theme.Base.AppCompat.Light.DarkActionBar"> 

只需将其更改为

<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar">

如果您只想将后退按钮和 X 图标改为白色,则对我有用。


2022年 - 解决方案仍然适用于API 32。 - danyapd

2

1:要更改后退按钮图标,添加以下内容:

app:collapseIcon="@drawable/back_arrow"

android.support.v7.widget.Toolbar中:

2: 要更改清除按钮图标,请添加

ImageView searchClose = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchClose.setImageResource(R.drawable.close);

onCreateOptionsMenu 中。

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