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

发布于 2024-12-17 19:32:16 字数 1532 浏览 4 评论 0原文

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

我已经放入 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

I am trying to create a button where I can hide or show the status bar on my tablet.

I've put in the onCreate

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

and in the buttons
show:

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

hide:

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

Any hints/tipps?

//edit

I've looked at this hint here: http://android.serverbox.ch/?p=306
and changed my code like this:

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

So if I click on my buttons an the methods are called I can see that something is happening because the app is waiting some seconds.
I also looked into LockCat and see that something is happening.

show: http://pastebin.com/CidTRSTi
hide: http://pastebin.com/iPS6Kgbp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

等往事风中吹 2024-12-24 19:32:16

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

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

Do you have the fullscreen theme set in the manifest?

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

I don't think you'll be able to go fullscreen without this.

I would use the following to add and remove the fullscreen flag:

// Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Show status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
拥醉 2024-12-24 19:32:16

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

这是对我有用的解决方案,(文档) (标志参考)

隐藏状态栏

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

For Some People, Showing status bar by clearing FLAG_FULLSCREEN may not work,

Here is the solution that worked for me, (Documentation) (Flag Reference)

Hide Status Bar

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

Show Status Bar

   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);
    }
半﹌身腐败 2024-12-24 19:32:16

用于android中的kolin
的行尾使用分号(;)

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

在kolin中隐藏状态栏不需要在android中使用java语言隐藏状态栏

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

used for kolin in android
for hide status bar in kolin no need to used semicolon(;) at the end of the line

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

in android using java language for hid status bar

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
关于从前 2024-12-24 19:32:16

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

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

并再次显示它:

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

I know its a very old question, but just in case anyone lands here looking for the newest supported way to hide status bar on the go programmatically, you can do it as follows:

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

and to show it again:

window.insetsController?.show(WindowInsets.Type.statusBars())
原谅我要高飞 2024-12-24 19:32:16

我已经尝试了很多事情。

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

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 故障

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

I've tried so many things.

Finally, It is the most suitable code to hide and show full screen mode.

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
    }

}

It Implemented it in this app : Android Breakdown.

Go to Videos(Bottom Bar) > Play Any Video > Toggle Fullscreen

止于盛夏 2024-12-24 19:32:16
 @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);
}
 @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);
}
肥爪爪 2024-12-24 19:32:16

对于 Kotlin 用户

显示

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

隐藏

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

For Kotlin users

TO SHOW

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

TO HIDE

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

Saygoodbye 2024-12-24 19:32:16

使用此方法,使用 SYSTEM_UI_FLAG_IMMERSIVE_STICKY 无需任何实现即可一键返回全屏。只需复制下面的这个方法并在您的活动中您想要的地方调用它即可。
更多详细信息此处

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

with this method, using SYSTEM_UI_FLAG_IMMERSIVE_STICKY the full screen come back with one tap without any implementation. Just copy past this method below and call it where you want in your activity.
More details 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);
}
憧憬巴黎街头的黎明 2024-12-24 19:32:16

添加到 XML FitsSystemWindows 参数:

Add to XML fitsSystemWindows parameter:

<RelativeLayout
...
android:fitsSystemWindows="true">

送你一个梦 2024-12-24 19:32:16

参考 - 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);
}

Reference - 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);
}
分分钟 2024-12-24 19:32:16
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)
    }
}
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)
    }
}
深海夜未眠 2024-12-24 19:32:16

如果您只想隐藏状态栏,请使用此

window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

window?.statusBarColor = Color.TRANSPARENT

If you want to hide only the Status bar, use this

window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

window?.statusBarColor = Color.TRANSPARENT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文