繁体   English   中英

Android R:API 30:系统栏可见性更改回调

[英]Android R : API 30 : System bar visibility change call backs

我正在尝试使我的应用程序成为全屏应用程序。 在 Api 级别 29 及以下,我使用onSystemUIVisibilityChange侦听器在应用程序全屏时处理我的活动中某些视图的可见性。 但是在 api 级别 30 上,是否有类似的回调来了解系统栏(状态栏和导航栏)的可见性何时发生变化? 由于 API 级别 29 及更高版本不推荐使用onSystemUIVisibilityChange

我知道activity.window.insetsController.show(WindowInsets.Type.systemBars())来显示和隐藏系统栏。 我需要的是系统栏可见性改变时的回调。

正如文档中所述,您可以使用设置侦听器
View.OnApplyWindowInsetsListener((view: View, window:WindowInsets) -> WindowInsets)
然后在侦听器中,您可以使用WindowInsets.isVisible(Int)检查系统栏是否可见。 像这样:

private val onWindowInsetApplied: (view: View, window:WindowInsets) -> WindowInsets = { view, windowInsets ->
    val areSystemBarsVisible = windowInsets.isVisible(WindowInsets.Type.systemBars())
    //Do something
    windowInsets
}

fun setListener() {
    View.OnApplyWindowInsetsListener(onWindowInsetApplied)
}

对于 Java 开发人员,这是@ Kyzer Soze答案的java版本

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

    decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {

            if (insets.isVisible(WindowInsets.Type.systemBars())) {
                Toast.makeText(MainActivity.this, "system bars are Visible",    
                                                       Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "system bars are Invisible", 
                                                       Toast.LENGTH_SHORT).show();
            }

            return insets;
        }
    });

}

确保不要在 API-30 以下调用setOnApplyWindowInsetsListener ,因为它会引发运行时异常

暂无
暂无

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

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