【问题标题】:Android: show/hide status bar/power barAndroid:显示/隐藏状态栏/电源栏
【发布时间】:2012-01-06 13:47:36
【问题描述】:

我正在尝试创建一个按钮,我可以在其中隐藏或显示平板电脑上的状态栏。

我已经加入了 onCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

在按钮中 显示:

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);

隐藏:

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);

任何提示/提示?

//编辑

我在这里看到了这个提示:http://android.serverbox.ch/?p=306 并像这样更改了我的代码:

private void hideStatusBar() throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"});
    proc.waitFor();
}

private void showStatusBar() throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});
    proc.waitFor();
}

因此,如果我单击按钮并调用方法,我可以看到正在发生的事情,因为应用程序正在等待几秒钟。 我还查看了 LockCat,发现发生了一些事情。

显示:http://pastebin.com/CidTRSTi 隐藏:http://pastebin.com/iPS6Kgbp

【问题讨论】:

标签: android show-hide statusbar


【解决方案1】:

您是否在清单中设置了全屏主题?

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

如果没有这个,我认为你无法全屏显示。

我将使用以下内容来添加和删除全屏标志:

// Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Show status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

【讨论】:

  • 不,我没有将它添加到清单中。但是当我尝试时,我收到错误:错误:找不到与给定名称匹配的资源(在'主题',值为'@android:style/ Theme.Dark.NoTitleBar.Fullscreen')。”
  • @B770:没有名为Theme.Dark.NoTitleBar.Fullscreen。有一个名为Theme.NoTitleBar.Fullscreen。但是,您无法摆脱缺少屏幕外 HOME 和 BACK 按钮的 Android 3.0+ 设备上的系统栏。
  • @CommonWare:我发现了这个:forum.xda-developers.com/showthread.php?t=1265397 这里可以隐藏和显示状态栏。我已经安装了包含此功能的“克服”ROM,并且可以正常工作。所以我认为也可以将此功能放在其他按钮上......
  • 好的。我懂了。我忘记授予该应用程序的 su 权限。现在一切正常:)
  • 上下滚动时是否可以显示/隐藏状态栏?
【解决方案2】:

对于某些人,通过清除 FLAG_FULLSCREEN 显示状态栏可能不起作用,

这是对我有用的解决方案,(Documentation) (Flag Reference)

隐藏状态栏

// Hide Status Bar
if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
   View decorView = getWindow().getDecorView();
  // Hide Status Bar.
   int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
   decorView.setSystemUiVisibility(uiOptions);
}

显示状态栏

   if (Build.VERSION.SDK_INT < 16) {
              getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else {
       View decorView = getWindow().getDecorView();
      // Show Status Bar.
       int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
       decorView.setSystemUiVisibility(uiOptions);
    }

【讨论】:

  • 我有完全相同的代码,但状态栏仍在显示
【解决方案3】:

用于 android 中的 kolin 在 kolin 中隐藏状态栏不需要在行尾使用分号(;)

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

在android中使用java语言隐藏状态栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

【讨论】:

  • 只隐藏StatusBar,不显示。
【解决方案4】:

我知道这是一个非常古老的问题,但以防万一有人在这里寻找最新支持的方式以编程方式在旅途中隐藏状态栏,您可以按以下方式进行:

window.insetsController?.hide(WindowInsets.Type.statusBars())

并再次显示它:

window.insetsController?.show(WindowInsets.Type.statusBars())

【讨论】:

    【解决方案5】:

    对于 Kotlin 用户

    展示

    activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

    隐藏

    activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

    【讨论】:

      【解决方案6】:

      我尝试了很多东西。

      最后,最适合隐藏和显示全屏模式的代码。

      private fun hideSystemUi() {
      
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
              window.setDecorFitsSystemWindows(true)
          } else {
              // hide status bar
              getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
              window.decorView.systemUiVisibility =
                  View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
          }
      
      }
      
      private fun showSystemUi() {
      
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
              window.setDecorFitsSystemWindows(false)
          } else {
              // Show status bar
              window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
              window.decorView.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_STABLE
          }
      
      }
      

      它在这个应用程序中实现了它:Android Breakdown

      转到视频(底部栏)> 播放任何视频> 切换全屏

      【讨论】:

        【解决方案7】:
         @Override
         protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //hide status bar
            requestWindowFeature( Window.FEATURE_NO_TITLE );
            getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN );
        
        
            setContentView(R.layout.activity_main);
        }
        

        【讨论】:

          【解决方案8】:

          使用此方法,使用 SYSTEM_UI_FLAG_IMMERSIVE_STICKY 一键返回全屏,无需任何实现。只需在下面复制此方法并在活动中所需的位置调用它即可。 更多详情here

          private void hideSystemUI() {
              getWindow().getDecorView().setSystemUiVisibility(
                      View.SYSTEM_UI_FLAG_IMMERSIVE
                              // Set the content to appear under the system bars so that the
                              // content doesn't resize when the system bars hide and show.
                              | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                              | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                              | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                              // Hide the nav bar and status bar
                              | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                              | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                              | View.SYSTEM_UI_FLAG_FULLSCREEN);
          }
          

          【讨论】:

            【解决方案9】:

            参考 - https://developer.android.com/training/system-ui/immersive.html

            // This snippet shows the system bars. It does this by removing all the flags
            // except for the ones that make the content appear under the system bars.
            private void showSystemUI() {
                mDecorView.setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
            

            【讨论】:

              【解决方案10】:
              fun Activity.setStatusBarVisibility(isVisible: Boolean) {
                  //see details https://developer.android.com/training/system-ui/immersive
                  if (isVisible) {
                      window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                              or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
                      window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                              or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
                  } else {
                      window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                              or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                              or View.SYSTEM_UI_FLAG_FULLSCREEN)
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-10-14
                • 2016-03-29
                • 1970-01-01
                • 1970-01-01
                • 2016-05-10
                • 1970-01-01
                • 2014-04-23
                • 1970-01-01
                相关资源
                最近更新 更多