【问题标题】:Extend view under status bar on Android 11在 Android 11 上的状态栏下扩展视图
【发布时间】:2021-08-12 15:18:42
【问题描述】:

我们一直在尝试迁移我们的代码库以针对 API 30 进行编译(不使用弃用),但在处理系统 UI 和窗口插图的广泛弃用时遇到了一些问题。

我们正在尝试在持久状态栏下绘制内容,而不是在导航栏下。 (所以setDecorFitsSystemWindows 不起作用)

以前我们是这样做的:


activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
activity?.window?.decorView?.systemUiVisibility?.let {
    activity?.window?.decorView?.systemUiVisibility = it and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}

binding.start.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)

我已经能够通过

让状态栏一直变亮
window.insetsController?.setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)

使用插入控制器,但我不知道如何让视图在状态栏下向上扩展。

我尝试弄乱插图,但 setOnApplyWindowInsetsListener 不再被调用以进行修改。

查看整个网络,我没有看到任何不推荐的方式来获得这种行为。我想知道这是否还有可能?

【问题讨论】:

  • 所以基本上你希望你的活动画在一个透明的状态栏后面?

标签: android android-11 android-statusbar


【解决方案1】:

风格

<item name="android:windowTranslucentStatus">true</item>

在清单中

android:fitsSystemWindows="true"

在我的 Android 11 中,状态栏图标颜色在滚动时也会变亮和变暗。

【讨论】:

    【解决方案2】:

    我偶然发现了这个关于 Android 11 insets 的资源,并且能够让它工作: https://blog.stylingandroid.com/android11-windowinsets-part1/

    获得我想要的外观的最终兼容版本是这样的:

            // force view to be full screen and appearance to be light
            //
            activity?.window?.let { window ->
                WindowCompat.setDecorFitsSystemWindows(window, false)
                window.statusBarColor = Color.TRANSPARENT
                val controller = WindowInsetsControllerCompat(window, binding.root)
                controller.isAppearanceLightNavigationBars = false
            }
            // Adjust view to be under status bar
            //
            binding.root.setOnApplyWindowInsetsListener { view, windowInsets ->
                val insetsCompat = WindowInsetsCompat.toWindowInsetsCompat(windowInsets, view)
                val insets = insetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars())
                binding.root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                    updateMargins(insets.left, insets.top, insets.right, insets.bottom)
                }
                WindowInsetsCompat.Builder()
                    .setInsets(WindowInsetsCompat.Type.navigationBars(), insets)
                    .build().toWindowInsets() ?: windowInsets
            }
    
    
    

    我学到的重要一点是,如果您将DecorFitsSystemWindows 设置为false,那么将调用onApplyWindowInsets ,它允许您自定义插入视图,但您愿意。

    这是有效的,并且从我们的 api 26 到 30 的最小值给出了相同(正确)的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 2017-11-30
      • 2014-06-19
      • 1970-01-01
      • 2018-05-30
      • 2015-03-12
      • 2022-12-21
      相关资源
      最近更新 更多