How to show/hide Actionbar when clicked on [closed

2020-02-11 08:02发布

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? :)

2条回答
太酷不给撩
2楼-- · 2020-02-11 08:48

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.

查看更多
▲ chillily
3楼-- · 2020-02-11 08:49

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

查看更多
登录 后发表回答