自定义全屏行为

发布于 2025-01-11 07:16:48 字数 834 浏览 1 评论 0原文

根据 android 文档,我们可以通过在 onCreate 方法中添加以下代码块(在 setContent{...} 内部,具体来说,如果您使用的是 Compose)

val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView)
windowInsetsController.systemBarsBehavior =
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
            

但是,这似乎只隐藏了状态栏上显示的信息,仅用黑色条纹替换它。

现在,我的问题是 - 我们如何修改该条纹的颜色,以便应用程序的 UI 看起来延伸到整个显示屏?

我不知道从哪里开始,但我听说伴奏者可能有与此相关的东西,但我认为最好在这里发布这个问题,这样如果有人已经知道解决方法,他们可以分享,因为这将有助于社区。

除此之外,不涉及伴奏的解决方案也是受欢迎的,甚至可能是首选。

作为参考,这是现在的输出

Preview

注意到顶部的黑条了吗?这就是目标。

Per the android docs, we can make an activity go full-screen by adding the following block of code in the onCreate method (inside setContent{...}, specifically, if you are using Compose)

val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView)
windowInsetsController.systemBarsBehavior =
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
            

However, this seems to only hide the information displayed on the statusbar, replacing it with just a black stripe.

Now, my question is - How can we modify the color of this stripe so that the UI of the app seems to extend to the entire display?

I am not sure where to start but I've heard accompanist MAY have something related to this, but I thought it better to post this question here, so that if anyone already knows a way around, they may share since it will be helpful to the community.

Other than that, solutions that do not involve accompanist are also welcome, and may be even preferred.

For reference, here's the output as of now

Preview

Notice the black bar at the top? That's the target.

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

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

发布评论

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

评论(1

亽野灬性zι浪 2025-01-18 07:16:48

复制并粘贴到 onCreate 方法中,结果你可以理解

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val window: Window = window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        val decorView: View = window.getDecorView()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
        window.setStatusBarColor(Color.TRANSPARENT)
    }

copy and paste it in onCreate method after result you can understand

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val window: Window = window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        val decorView: View = window.getDecorView()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
        window.setStatusBarColor(Color.TRANSPARENT)
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文