首页 > 解决方案 > getStableInsetTop() 总是返回 0?

问题描述

我想获得稳定的系统 UI 插图。从中获取系统 UI(导航栏和状态栏)的高度和宽度。

问题:我使用了getSystemWindowInsetTop()windowInsets 类的方法,但是当屏幕进入沉浸式模式并且系统 UI(导航栏和状态栏)隐藏时它变为零。但我想要稳定的插图,这就是我尝试使用getStableInsetTop()方法的原因,它总是返回 0。

代码:

binding.lytParent.setOnApplyWindowInsetsListener((view, windowInsets) -> {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT
                        , windowInsets.getStableInsetTop()
                );
                binding.vTopMargin.setLayoutParams(params);
});

注意:我已经fitSystemWindows="true"在 XML 的父布局中添加了该属性。getSystemWindowInsetTop()工作正常并返回 Inset 值,但问题是它的值随系统 UI(导航栏和状态栏)的可见性而变化。

标签: androidandroid-layoutwindowinsets

解决方案


推荐阅读