安卓搜索视图返回按钮颜色更改

6
如何更改搜索视图中黑色箭头(返回按钮)的颜色。 我尝试使用以下代码进行自定义:
    ImageView backid = (ImageView) searchViewAndroidActionBar.findViewById(R.id.search_button);
    backid.setColorFilter(ContextCompat.getColor(Shopping_CategoriesCommon.this, R.color.white), PorterDuff.Mode.SRC_IN);
    backid.setImageResource(R.drawable.search);

但它不起作用


请参考以下内容:如何更改新材料主题中返回箭头的颜色。 - sasikumar
3个回答

16
在 XML 文件中,添加以下属性到你的工具栏: app:collapseIcon
<android.support.v7.widget.Toolbar
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="@dimen/toolbarHeight"
     app:collapseIcon="@drawable/collapseBackIcon" />

12

经过一天的搜索,我解决了这个问题,只需将app:collapseIcon="@drawable/back_arrow"添加到自定义工具栏中即可。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    app:collapseIcon="@drawable/back_arrow"/>

我们能在运行时设置collapseIcon吗? - Saurabh

4

我可以使用反射改变颜色。

try {
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    Field mCollapseIcon = toolbar.getClass().getDeclaredField("mCollapseIcon");
    mCollapseIcon.setAccessible(true);
    Drawable drw = (Drawable) mCollapseIcon.get(toolbar);
    drw.setTint(color);
}
catch (Exception e) {
}

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