简体   繁体   中英

Remove back button from SearchView in toolbar AppCompat

在此输入图像描述

How can I remove the back button icon when search view shows up in Toolbar (AppCompat)?

toolbar = (Toolbar) findViewById(R.id.tool_bar);


    // Set an OnMenuItemClickListener to handle menu item clicks
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Handle the menu item
            return true;
        }
    });

    // Inflate a menu to be displayed in the toolbar
    toolbar.inflateMenu(R.menu.menu_main);
    toolbar.setTitle("");


    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();

    // this works for normal back button but not for one appears on tapping SearchView
    actionBar.setDisplayHomeAsUpEnabled(false);

Use:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

toolbar.setNavigationIcon(null);

OR

toolbar.setNavigationIcon(getResources().getColor(android.R.color.transparent));
getSupportActionBar().setDisplayHomeAsUpEnabled(false);

There is no way to remove the back arrow from a SearchView. you can try finding it yourself in the class (of the support.v7 lib or the main Android project ) it looks like this:

mCloseButton = (ImageView) findViewById(R.id.search_close_btn);

But it's a private member and the visibility changes upon text input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM