简体   繁体   English

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

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

I am trying to make my app a full screen app.我正在尝试使我的应用程序成为全屏应用程序。 On Api levels 29 and below, I am using onSystemUIVisibilityChange listener to handle visibility of certain views in my activity when the app is in full screen.在 Api 级别 29 及以下,我使用onSystemUIVisibilityChange侦听器在应用程序全屏时处理我的活动中某些视图的可见性。 But on api level 30, is there any similar callback to know when system bars(status bar and navigation bar) visibility changes?但是在 api 级别 30 上,是否有类似的回调来了解系统栏(状态栏和导航栏)的可见性何时发生变化? Since onSystemUIVisibilityChange is deprecated for API level 29 and above.由于 API 级别 29 及更高版本不推荐使用onSystemUIVisibilityChange

I am aware of activity.window.insetsController.show(WindowInsets.Type.systemBars()) to show and hide System bars.我知道activity.window.insetsController.show(WindowInsets.Type.systemBars())来显示和隐藏系统栏。 What I need is a callback when system bar visibility changes.我需要的是系统栏可见性改变时的回调。

As mention in the documentation you can set a listener using正如文档中所述,您可以使用设置侦听器
View.OnApplyWindowInsetsListener((view: View, window:WindowInsets) -> WindowInsets)
and then in the listener you can check whether the system bars are visible or not using WindowInsets.isVisible(Int) .然后在侦听器中,您可以使用WindowInsets.isVisible(Int)检查系统栏是否可见。 Like so:像这样:

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)
}

For Java developers, this is the java version of @ Kyzer Soze answer对于 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;
        }
    });

}

Make sure not to call setOnApplyWindowInsetsListener in below API-30, as it will raise runtime exception确保不要在 API-30 以下调用setOnApplyWindowInsetsListener ,因为它会引发运行时异常

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

相关问题 在 Android 11 (API 30) 中以编程方式更改状态栏文本颜色 - Programmatically Change Status Bar Text Color in Android 11 (API 30) 服务回调到android中的活动 - Service call backs to activity in android Xamarin Forms 如何在 ZDB974238714CA8DE634A7CE01D01A1 中更改 Android 状态栏图标颜色 - Xamarin Forms How to change Android status bar icon color in API 30 (Android 11) 如何在 Android 11(API 级别 30)中更改 AppCompatDialogFragment 的状态栏颜色 - How to change status bar color of AppCompatDialogFragment in Android 11 (API level 30) 如何创建文件夹(Android R - Api 30)? - How to create folder (Android R - Api 30)? Facebook SDK:“以 Android API 30+ (Android 11+) 为目标的应用无法调用 Facebook 本机应用,除非声明 package 可见性需求” - Facebook SDK : 'Apps that target Android API 30+ (Android 11+) cannot call Facebook native apps unless the package visibility needs are declared' 在Android api 30中滑动时状态栏变为黑色并且图标可见 - status bar becoming black and icon visible on swiping in Android api 30 Android 11 (API 30) 中的导航栏不完全透明 - Navigation bar is not fully transparent in Android 11 (API 30) 如何正确隐藏 Android API 30 的状态栏? - How to properly hide the status bar for Android API 30? android:如何从服务到活动回调? - android:How to get call backs from service to activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM