在 Android 中更改 SearchView 后退按钮

Change SearchView backbutton in Android

我有白色的操作栏,当我点击搜索视图按钮时,我能够获得搜索视图但无法获得搜索视图下方的行并且看不到后退按钮。我想更改搜索视图后退按钮的 color/complete 图标,我想获取搜索视图下方的行。我用谷歌搜索并尝试了很多不同的方法,但没有成功。我也尝试过更改工具栏样式,但没有成功。我也在活动和片段中使用搜索视图..

我试过像下面这样更改工具栏样式

 <style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    <!-- Customize color of navigation drawer icon and back arrow -->
    <item name="colorControlNormal">@color/colorBlack</item>
    <item name="android:collapseIcon" tools:ignore="NewApi">@mipmap/filter_red</item>
</style>

如有帮助将不胜感激!!!!

提前致谢

(i) 您可以在 Toolbar.

中包含 app:collapseIcon 属性
<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="2dp"
            app:collapseIcon="@drawable/cancel"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

(ii) 另一种方法是在 AppBarLayout 的帮助下以编程方式执行此操作。

AppBarLayout appBar =  findViewById(R.id.appBarLayout);
        appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                for (int i = 0; i < toolbar.getChildCount(); i++) {
                    View view = toolbar.getChildAt(i);
                    if (view instanceof ImageButton) {
                        ImageButton btn = (ImageButton) view;
                        btn.setImageDrawable(getResources().getDrawable(R.drawable.cancel)); // Here we can change icon.
                    }
                }
            }
        });

布局如下:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="2dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>