Android:显示/隐藏状态栏/电源栏

Android:显示/隐藏状态栏/电源栏,android,show-hide,statusbar,Android,Show Hide,Statusbar,我正在尝试创建一个按钮,以便在平板电脑上隐藏或显示状态栏 我已经加入了onCreate getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 在按钮上 展示: WindowManager.LayoutParams attrs = getWindow().getAt

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

我已经加入了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);
隐藏:

有什么提示/提示吗

//编辑

我在这里看到了这个提示: 并将我的代码更改为:

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,发现有什么事情发生了

展示:
隐藏:

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

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);

对于某些人来说,通过全屏清除FLAG_来显示状态栏可能不起作用

这是对我有效的解决方案,()()

隐藏状态栏

// 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);
    }
//隐藏状态栏
如果(Build.VERSION.SDK_INT<16){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
}
否则{
View decorView=getWindow().getDecorView();
//隐藏状态栏。
int uiOptions=View.SYSTEM\u UI\u FLAG\u全屏显示;
decorView.设置系统兼容性(uiOptions);
}
显示状态栏

// 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);
    }
if(Build.VERSION.SDK\u INT<16){
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_全屏);
}
否则{
View decorView=getWindow().getDecorView();
//显示状态栏。
int uiOptions=View.SYSTEM\u UI\u FLAG\u可见;
decorView.设置系统兼容性(uiOptions);
}
参考-

适用于Kotlin用户

显示

activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_全屏)

隐藏

activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_全屏)

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

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
在android中使用java语言进行hid状态栏

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

使用这种方法,使用SYSTEM_UI_FLAG_IMMERSIVE_STICKY,只需轻触一下即可返回全屏,无需任何实现。只需复制下面的方法,并在活动中的任何位置调用它。 更多细节


我试过很多东西

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

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
    }

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


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

我知道这是一个非常老的问题,但为了防止任何人在这里寻找最新的支持方式以编程方式隐藏状态栏,您可以按如下操作:

window.insetsController?.hide(WindowInsets.Type.statusBars())
再次展示:

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

不,我没有把它添加到舱单上。但是当我尝试时,我得到了一个错误:错误:找不到与给定名称匹配的资源(在'theme'处,值为'@android:style/theme.Dark.NoTitleBar.Fullscreen')。“@B770:没有一个名为
主题.Dark.NoTitleBar.Fullscreen
。有一个名为
Theme.NoTitleBar.Fullscreen
。但是,在Android 3.0+设备上,您无法摆脱缺少屏幕外HOME和BACK按钮的系统栏。@CommonWare:我发现:在这里可以隐藏和显示状态栏。我已经安装了“克服”ROM,其中包括这项功能,它的工作。所以我想也可以把这个功能放在另一个按钮上…好的。我知道了。我忘了授予su应用程序的权限。现在一切正常:)上下滚动时是否可以显示/隐藏状态栏?例如,我有相同的代码,但状态栏仍然显示
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
    }

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