【问题标题】:How to toggle the Statusbar?如何切换状态栏?
【发布时间】:2010-10-04 14:57:55
【问题描述】:

我正在寻找一种使用onClickListener 显示和隐藏状态栏的方法,但只显示它有效。

WindowManager.LayoutParams lp = getWindow().getAttributes();
if (isStatusbarVisible)
    lp.flags = LayoutParams.FLAG_FULLSCREEN;
else
    lp.flags = LayoutParams.FLAG_FORCE_NOT_FULLSCREEN;
getWindow().setAttributes(lp);
isStatusbarVisible = !isStatusbarVisible;

使用FLAG_FULLSCREEN 隐藏状态栏似乎只有在调用setContentView() 之前设置了标志才有效。

还有其他隐藏状态栏的方法吗?

【问题讨论】:

    标签: android statusbar


    【解决方案1】:

    答案很简单,只需清除FLAG_FULLSCREEN 标志即可:

    if (isStatusBarVisible)
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    else
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    【讨论】:

      【解决方案2】:

      我一直在寻找这个解决方案,最后想出了这个实现来打开和关闭全屏:

      private void toggleFullscreen() {
          WindowManager.LayoutParams attrs = getWindow().getAttributes();
          attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN;
          getWindow().setAttributes(attrs);
      }
      

      它使用按位异或逻辑来切换FLAG_FULLSCREEN

      【讨论】:

        【解决方案3】:

        正如我们所见,Android API 在不断变化。这是该问题的最新答案。

        根据此文档:https://developer.android.com/training/system-ui/status.html

        在 Android 4.1 及更高版本上隐藏状态栏

        您可以隐藏状态 在 Android 4.1(API 级别 16)及更高版本上使用 setSystemUiVisibility()。 setSystemUiVisibility() 在 个人视图级别;这些设置汇总到窗口 等级。使用 setSystemUiVisibility() 设置 UI 标志会给你更多 比使用 WindowManager 标志更精细地控制系统栏。 这个 sn -p 隐藏状态栏:

        View decorView = getWindow().getDecorView();
        // Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        // Remember that you should never show the action bar if the
        // status bar is hidden, so hide that too if necessary.
        ActionBar actionBar = getActionBar();
        actionBar.hide();
        

        从其他答案中借用 groovy XOR:

        public void toggleFullscreen() {
            View decorView = getWindow().getDecorView();
        
            int uiOptions = decorView.getSystemUiVisibility();
        
            uiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
        
            decorView.setSystemUiVisibility(uiOptions);
        }
        

        这确实有效(我正在使用它)并且似乎与getWindow().setAttributes() 解决方案的功能相同,但这是开发人员文档中推荐的方式。

        【讨论】:

          【解决方案4】:

          有一个更好的(不需要布尔变量)切换全屏方法实现:

          private void toggleFullscreen() {
              WindowManager.LayoutParams attrs = getWindow().getAttributes();
              attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN;
              getWindow().setAttributes(attrs);
          }
          

          它使用按位异或逻辑来切换FLAG_FULLSCREEN

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-23
            • 2014-10-09
            • 2011-10-03
            • 2011-12-19
            • 1970-01-01
            相关资源
            最近更新 更多