How to show/hide Actionbar when clicked on [closed]

一世执手 提交于 2019-11-28 01:26:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!