简体   繁体   English

无法在 Android 11+ 上获取 systemBars 插图

[英]Trouble getting systemBars Insets on Android 11+

I'm fairly new to Android, and I've been experimenting with edge-to-edge content as described in this guide just making it work with a basic RecyclerView that scrolls behind a transparent navigation bar.我是 Android 的新手,我一直在试验本指南中所述的边到边内容,只是让它与在透明导航栏后面滚动的基本 RecyclerView 一起使用。 As the guide instructs, I use window insets to adjust padding and prevent overlap:按照指南的指示,我使用 window 插图来调整填充并防止重叠:

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.layout_root), (v, windowInsets) -> {
            Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
            Log.d("DEBUG", "Insets: " + insets.toString());
            v.setPadding(0, insets.top, 0, insets.bottom);
            return WindowInsetsCompat.CONSUMED;
        });

Everything works perfectly on Android 10, but the top inset always returns 0 when testing on Android 11 or 12. So far I've found a lot about changes to Window Insets in Android 11, but I can't find anything pertaining to this particular issue.一切都在 Android 10 上完美运行,但在 Android 11 或 12 上测试时,顶部插图总是返回 0。到目前为止,我发现了很多关于 Android 11 中 Window 插图的更改,但我找不到与此特定相关的任何内容问题。

I faced the same issue as well.我也遇到了同样的问题。 It always returned 0.它总是返回 0。

I fixed my issue with the following:我解决了以下问题:

int insetTypes = WindowInsetsCompat.Type.displayCutout() | WindowInsetsCompat.Type.systemBars();
Insets insets = windowInsets.getInsets(insetTypes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM