android.view.View.systemUiVisibility deprecated. What is the replacement?

前端 未结 6 1133
自闭症患者
自闭症患者 2020-12-10 00:49

I have updated the project target API version to 30, and now I see that the systemUiVisibility property is deprecated.

The following kotlin code is the one I\'m using

相关标签:
6条回答
  • 2020-12-10 01:28

    Since you probably want to have a minSdkVersion less than 30 I'd recommend using WindowCompat and WindowInsetsControllerCompat. You'll need to upgrade your gradle dependency for androidx.core to at least 1.5.0-alpha02

    You can find out more information about WindowInsets by watching this YouTube video

    private fun hideSystemUI() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        WindowInsetsControllerCompat(window, mainContainer).let { controller ->
            controller.hide(WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars())
            controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }
    
    private fun showSystemUI() {
        WindowCompat.setDecorFitsSystemWindows(window, true)
        WindowInsetsControllerCompat(window, mainContainer).show(WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars())
    }
    
    0 讨论(0)
  • 2020-12-10 01:35

    As of version 1.5.0-alpha02, androidx.core has WindowCompat.setDecorFitsSystemWindows()

    To enable edge-to-edge:

    WindowCompat.setDecorFitsSystemWindows(window, false)
    
    0 讨论(0)
  • 2020-12-10 01:35

    TL;DR snippet

    Wrapping in if-else structure needed to avoid java.lang.NoSuchMethodError: No virtual method setDecorFitsSystemWindows exception on older SDK versions.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.setDecorFitsSystemWindows(false)
    } else {
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
    }
    

    Links with full information about insets and fullscreen modes in Android 11

    https://blog.stylingandroid.com/android11-windowinsets-part1/

    https://www.youtube.com/watch?v=acC7SR1EXsI

    0 讨论(0)
  • 2020-12-10 01:44

    Also you may want to have a translucent status bar and you can do it simply through setting a style to your app's theme as follows:

    <style name="App.MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="android:windowLightStatusBar">true</item>
    </style>
    

    A lint message can be displayed (depending on you curren min api level): android:windowLightStatusBar requires API level 23 (current min is 21), so you need to override this theme in v23 styles

    0 讨论(0)
  • 2020-12-10 01:46

    I hope It helps you.

    Previously, when implementing edge-to-edge navigation or immersive mode, one of the first steps to take was to use the systemUiVisibility flags in order to request the app to be laid out fullscreen, This new Android release deprecates this field and in order to layout the app fullscreen you have to use a new method on the Window class: setDecorFitsSystemWindows passing false as an argument like below.

    window.setDecorFitsSystemWindows(false)
    

    WindowInsetsController class which allows you to do things that previously were controlled via systemUiVisibility flags, like hiding or showing the status bar or navigation bar(hide and show methods, respectively)

    For example, you can easily show and hide the keyboard as shown below:

    // You have to wait for the view to be attached to the
    // window (otherwise, windowInsetController will be null)
    view.doOnLayout {
        view.windowInsetsController?.show(WindowInsets.Type.ime())
        // You can also access it from Window
        window.insetsController?.show(WindowInsets.Type.ime())
    }
    
    0 讨论(0)
  • 2020-12-10 01:47
    WindowCompat.setDecorFitsSystemWindows(window, false)
    

    Watch this tutorial from official Android Developers channel.

    0 讨论(0)
提交回复
热议问题