代码之家  ›  专栏  ›  技术社区  ›  user246392

Xamarin Android:如何在Android 11中设置SystemUiFlags[重复]

  •  0
  • user246392  · 技术社区  · 5 年前

    我已将项目目标API版本更新为30,现在我看到systemUiVisibility属性已被弃用。

    下面的kotlin代码是我正在使用的代码,它实际上相当于 setSystemUiVisibility Java中的方法。

    playerView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
    

    如果您有任何稳定的替代品来替换此已弃用的代码,请告诉我。谷歌的建议是使用 WindowInsetsController 但我不知道该怎么做。

    0 回复  |  直到 6 年前
        1
  •  5
  •   Kishan Solanki    4 年前

    为了兼容性,请使用 WindowCompat 和 WindowInsetsControllerCompat 。您需要升级您的gradle依赖项 androidx.core 至少 1.6.0-alpha03 以便得到支持 setSystemBarsBehavior 关于SDK<30.

    private fun hideSystemUI() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        WindowInsetsControllerCompat(window, mainContainer).let { controller ->
            controller.hide(WindowInsetsCompat.Type.systemBars())
            controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }
    
    private fun showSystemUI() {
        WindowCompat.setDecorFitsSystemWindows(window, true)
        WindowInsetsControllerCompat(window, mainContainer).show(WindowInsetsCompat.Type.systemBars())
    }
    

    您可以了解更多关于 WindowInsets 通过观看这个 YouTube video

    对于显示器顶部有凹槽的设备,您可以在v27中添加以下内容 theme.xml 文件使UI显示在凹口的两侧:

    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    

    您可以通过以下链接阅读更多内容: Display Cutout

        2
  •  2
  •   Husniddin Muhammad Amin    4 年前

    TL;DR片段

    如果需要避免,请包裹在结构中 java.lang.NoSuchMethodError: No virtual method setDecorFitsSystemWindows 旧SDK版本的异常。

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

    关于Android 11中嵌入式和全屏模式的完整信息链接

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

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

        3
  •  1
  •   LozCool    4 年前

    我希望它能帮助你。

    以前,在实现边到边导航或沉浸式模式时,要采取的第一步是使用systemUiVisibility标志来请求应用程序全屏布局, 这个新的Android版本弃用了此字段,为了全屏布局应用程序,您必须在 Window 类别: setDecorFitsSystemWindows 经过 false 就像下面这样的论点。

    window.setDecorFitsSystemWindows(false)
    

    WindowInsetsController 类,它允许你做以前通过以下方式控制的事情 systemUiVisibility 标志,如隐藏或显示状态栏或导航栏(分别为隐藏和显示方法)

    例如,您可以轻松显示和隐藏键盘,如下所示:

    // 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())
    }
    
        4
  •  1
  •   Amit    4 年前

    截至版本 1.5.0-alpha02 , androidx.core 有 WindowCompat.setDecorFitsSystemWindows()

    要启用边到边:

    WindowCompat.setDecorFitsSystemWindows(window, false)
    
        5
  •  0
  •   sweak    4 年前

    如果有人搜索Java版本。

    For Activity :

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        getWindow().setDecorFitsSystemWindows(false);
    
        if (getWindow().getInsetsController() != null) {
            getWindow().getInsetsController().hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
            getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
        }
    } else {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
    

    对于 FragmentDialog 和 AlertDialog :

    if (getDialog() != null && getDialog().getWindow() != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            getDialog().getWindow().setDecorFitsSystemWindows(false);
        } else {
            if (getActivity() != null) {
                getDialog().getWindow().getDecorView().setSystemUiVisibility(getActivity().getWindow().getDecorView().getSystemUiVisibility());
            }
        }
    }
    
        6
  •  0
  •   Doctiger sandip    4 年前

    对于Java用户(感谢@James):

    //hide system UI
    Window window = activity.getWindow();
    View decorView = activity.getWindow().getDecorView();
    
    WindowCompat.setDecorFitsSystemWindows(window, false);
    WindowInsetsControllerCompat controllerCompat = new WindowInsetsControllerCompat(window, decorView);
    controllerCompat.hide(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.navigationBars());
    controllerCompat.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    

    (编辑)使用 BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 仅在滑动时临时显示sytemBars, BEHAVIOR_SHOW_BARS_BY_SWIPE 滑动后将永久显示它们。

    来源: https://developer.android.com/reference/android/view/WindowInsetsController#BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

        7
  •  -1
  •   ItSNeverLate    4 年前
    WindowCompat.setDecorFitsSystemWindows(window, false)
    

    看这个 tutorial 来自官方Android开发者频道。