【问题标题】:Android Searchview back button color changeAndroid Searchview后退按钮颜色变化
【发布时间】:2017-06-01 12:47:30
【问题描述】:

如何在搜索视图中更改黑色箭头(后退按钮)的颜色 我试过用下面的代码自定义

    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);

但它不起作用

【问题讨论】:

标签: android searchview


【解决方案1】:

将此属性添加到 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" />

【讨论】:

    【解决方案2】:

    经过一天的搜索,我通过将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"/>
    

    【讨论】:

    • 我们可以在运行时设置 collpaseIcon 吗?
    【解决方案3】:

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

    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) {
    }
    

    【讨论】: