【问题标题】:systemWindowInsetBottom is always zerosystemWindowInsetBottom 始终为零
【发布时间】:2020-01-07 07:12:17
【问题描述】:

我将 setOnApplyWindowInsetsListener 设置为应用程序的根视图并获取导航栏更改的更新,但 insets.systemWindowInsetBottom 始终为 0

override fun onStart() {
    super.onStart()
    layoutMain?.setOnApplyWindowInsetsListener { v, insets ->
        layoutMain.updatePadding(bottom = insets.systemWindowInsetBottom)
        insets
    }
}

layoutMain 是 MaincActivity 中的约束布局和根视图组

<androidx.constraintlayout.widget.ConstraintLayout 
android:id="@+id/layoutMain"
android:fitsSystemWindows="true" ... >

在更多的事情上,我使用以下标志:

    window.addFlags(FLAG_LAYOUT_NO_LIMITS)

[using following tutorial :] 也是我的另一个question for the same problem

【问题讨论】:

  • 请尝试onResume()或onCreate()
  • 无所谓-_-是回调
  • 它与视图相关。并且您尝试从视图中获取回调。因此,在启动视图时尝试 onCreate 或 onResume :)
  • 还是一样的结果

标签: android configuration navigation android-10.0


【解决方案1】:

在创建示例项目并按照您的参考教程进行操作后,我发现了以下内容。

我找到了使systemWindowInsetBottom 值为零的原因。

  1. 该值取决于bottomNavigation。如果您的系统后退按钮(软层)与bottomNavigation 重叠,或者您的bottomNavigation 完全隐藏,那么您将获得正确的systemWindowInsetBottom 值。

  2. 但是如果你的bottomNavigation在系统软底层(返回按钮、主页按钮等)可见及以上,那么你将得到0。

下面的例子展示了我们什么时候可以得到零,什么时候我们可以得到实际值。

如果添加此行,您将获得一些价值,因为 bottomNavigation 不可见 mBinding.getRoot().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

但是,如果您删除该行或添加该行,您将始终得到零。 mBinding.getRoot().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

所以值取决于bottomNavigation的可见性状态

private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        mBinding.getRoot().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        if (navHostFragment != null) {
            NavigationUI.setupWithNavController(mBinding.bottomNavigation, navHostFragment.getNavController());
        }


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {

            mBinding.mainLayout.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                    Log.d("BottomTest", "padding: " + insets.getSystemWindowInsetBottom());

                    return insets;
                }
            });
        }
    }

【讨论】:

    【解决方案2】:

    从View的源码我们知道,只有你设置了影响系统UI布局的标志(SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN),你才能得到实际值。

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 2016-05-02
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多