如何更改SearchView后退按钮

时间:2017-09-20 23:24:47

标签: android android-toolbar searchview

我的工具栏上有一个SearchView,我想更改默认后退按钮的图标。

这是我的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<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/my_awesome_toolbar"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/actionBarSize"
    app:titleTextColor="@android:color/black"
    android:elevation="5dp">

</android.support.v7.widget.Toolbar>

这是我的SearchView的布局:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/search"
        android:title="@string/search"
        app:icon="@drawable/ic_search_black_24dp"
        android:icon="@drawable/ic_search_black_24dp"
        app:showAsAction="always|collapseActionView"
        android:maxWidth="50dp"
        app:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

这就是我在活动中创建选项菜单的方式:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search_team, menu);

    SearchManager searchManager = (SearchManager)
            getSystemService(Context.SEARCH_SERVICE);
    searchMenuItem = menu.findItem(R.id.search);

    searchView = (SearchView) searchMenuItem.getActionView();

    ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setImageResource(R.drawable.ic_action_cancel_black);
    searchClose.setVisibility(View.VISIBLE);

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchClose.getLayoutParams();
    params.gravity = Gravity.RIGHT;
    searchClose.setLayoutParams(params);

    searchClose.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //Clear query
            searchView.setQuery("", false);
            //Collapse the action view
            searchView.onActionViewCollapsed();
            //Collapse the search widget
            searchMenuItem.collapseActionView();

            listview.setVisibility(View.GONE);

            viewPager.bringToFront();

            appBarLayout.setVisibility(View.VISIBLE);
        }
    });

    for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
        textView.setTextColor(Color.BLACK);
        textView.setHintTextColor(Color.BLACK);
    }

    searchView.setSearchableInfo(searchManager.
            getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);

    return true;
}

这个输出是:

enter image description here

我想要的是在右侧隐藏此图标或至少将图标更改为我在我发布的有关我的活动的代码中定义为ImageView的“x”图像。

1 个答案:

答案 0 :(得分:4)

通过Style更改它是最简单的方法。

<style name="Theme.myCustomThemeForMyApp" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">@drawable/myCustomUpIndicator</item>

我将在此修改以进一步提供帮助。

   <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:actionMenuTextColor">@color/white</item>
    <item name="android:homeAsUpIndicator">@drawable/ic_account</item>
</style>

这是一个基本主题的正常示例,它修改了主要颜色,主要深色,重音,窗口背景动作菜单文本颜色,最后修改了homeAsUpIndicator drawable。

然后将其应用到您的活动或应用中:

<application
    android:name="com.appstudio35.application.A35Application"
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:roundIcon="@drawable/app_icon"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

您也可以选择将主题移动到子类,例如扩展父主题并从基本主题中删除up drawable以避免影响所有活动:

 <style name="AppTheme.CustomUp">
    <item name="android:homeAsUpIndicator">@drawable/ic_account</item>
</style>

并且只适用于这样的特定活动:

<activity
        android:name="com.appstudio35.activities.MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.CustomUp"/>