问题
When the user clicks anywhere on the screen, I want the Actionbar to hide and when pressed again it should reappear.
I know there is something called actionbar.hide(); and show, but can you please help me how to implement it? :)
回答1:
Just hide():
getActionBar().hide();
when you want to hide it, and use show():
getActionBar().show()
when you want to show it. That's about it.
Remember that if you're using View.SYSTEM_UI_FLAG_FULLSCREEN, this will not work properly.
回答2:
Try this. you have here the possibility to call hide or show method and according to your proposal
public class AbstractActivity Activity {
private boolean showActions = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getSupportActionBar();
if (bar != null) {
bar.setHomeButtonEnabled(true);
bar.setDisplayShowHomeEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
return true;
default:
// Nothing to do here
return super.onOptionsItemSelected(item);
}
}
private void handleActionBarTitle(boolean show) {
ActionBar actionBar = getSupportActionBar();
if (actionBar == null) {
return;
}
actionBar.setDisplayShowTitleEnabled(show);
}
protected void disableActions() {
this.showActions = false;
}
protected void enableActions() {
this.showActions = true;
}
protected void hideActionBarTitle() {
handleActionBarTitle(false);
}
protected boolean showActions() {
return showActions;
}
protected void showActionTitle() {
handleActionBarTitle(true);
}
Your activity just need to extends this AbstractActivity
来源:https://stackoverflow.com/questions/15327208/how-to-show-hide-actionbar-when-clicked-on