How to show/hide Actionbar when clicked on [closed

2020-02-11 08:06发布

问题:

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 6 years ago.

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