问题
Does anyone know how to hide the back button in AppCompat v21 searchview? (outlined by green line)
I've searched a lot but couldn't find anything useful.
menu_main.xml:
<item android:id="@+id/search"
android:title="@string/search_title"
app:showAsAction="always|collapseActionView"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
android:orderInCategory="300"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item android:id="@+id/action_home"
android:title="Home"
android:icon="@drawable/v_home"
app:showAsAction="always"
android:orderInCategory="180"/>
<item android:id="@+id/action_favorites"
android:title="Favorites"
android:icon="@drawable/v_favorites"
app:showAsAction="always" />
MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
firstMenu = menu;
searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setActivated(true);
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menuItemsVisibility(false);
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
menuItemsVisibility(true);
return false;
}
});
return true;
}
@Override
public void onBackPressed() {
menuItemsVisibility(true);
super.onBackPressed();
}
// setting visibility of menu items on search
private void menuItemsVisibility(boolean visibility) {
MenuItem homeItem = firstMenu.findItem(R.id.action_home);
MenuItem favoriteItem = firstMenu.findItem(R.id.action_favorites);
MenuItem otItem = firstMenu.findItem(R.id.action_ot);
MenuItem ntItem = firstMenu.findItem(R.id.action_nt);
homeItem.setVisible(visibility);
favoriteItem.setVisible(visibility);
otItem.setVisible(visibility);
ntItem.setVisible(visibility);
}
Note: the behavior showAsAction:Always and using methods menuItemsVisibility() to adjust the visibility of toolbar items is intentional.
Another Note: MainActivity extends ActionBarActivity and it also implements implements ObservableScrollViewCallbacks from ObservableScrollView Library.
回答1:
changed app:showAsAction="always|collapseActionView" to app:showAsAction="always"
回答2:
Use the method:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
in order to remove the home button from the action bar.
回答3:
It's not perfectly safe method because back button (navigation up) doesn't have id. But if you are using AppCompat with Toolbar, you can use this code to find it. It should be the first in the layout.
int count = this.getToolbar().getChildCount();
for(int i = 0; i < count; ++i) {
View v = this.getToolbar().getChildAt(i);
if(v instanceof ImageButton) {
return (ImageButton)v;
}
}
Call this method inside onPrepareOptionsMenu if you want to change drawable of that button.
来源:https://stackoverflow.com/questions/29294231/how-can-i-hide-the-back-button-in-searchview