• Android 沉浸式状态栏


    过时的API
    1. //设置默认隐藏虚拟按键,虚拟按键显示后为半透明
    2. protected open fun hideNavigationBarAndFullScreen() {
    3. val flags: Int
    4. // This work only for android 4.4+
    5. flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    6. // This work only for android 4.4+
    7. // hide navigation bar permanently in android activity
    8. // touch the screen, the navigation bar will not show
    9. window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) //虚拟按键透明
    10. (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    11. or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    12. or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    13. or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav
    14. // bar
    15. or View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
    16. or View.SYSTEM_UI_FLAG_IMMERSIVE)
    17. // flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    18. // | View.SYSTEM_UI_FLAG_IMMERSIVE
    19. // | View.SYSTEM_UI_FLAG_FULLSCREEN;
    20. } else {
    21. View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    22. }
    23. window.decorView.systemUiVisibility = flags
    24. }

    上面这一大堆,全都是过时的api,当然也能用

    新的方法
    theme
    1. <style name="Goscam_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    2. <item name="colorPrimary">@color/white</item>
    3. <item name="colorPrimaryDark">@color/white</item>
    4. <item name="colorAccent">@color/colorAccent</item>
    5. <item name="android:textAllCaps">false</item>
    6. <item name="android:textDirection">locale</item>
    7. <item name="android:windowTranslucentStatus">true</item>
    8. <item name="android:windowIsTranslucent">false</item>
    9. <item name="android:windowDisablePreview">true</item>
    10. <item name="android:windowTranslucentNavigation">false</item>
    11. <item name="android:windowLightStatusBar">true</item>
    12. </style>

    theme是必不可少的

    window
    1. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
    2. window.isNavigationBarContrastEnforced = false
    3. }
    4. window.statusBarColor = Color.TRANSPARENT
    5. window.navigationBarColor = Color.TRANSPARENT
    6. WindowCompat.setDecorFitsSystemWindows(window, true)

    设置状态栏颜色,设置底部导航栏颜色

    setDecorFitsSystemWindows

    函数就是用来替换过时的函数 setSystemUiVisibility

    setDecorfitsSystemWindows的解释

    1. /**
    2. * Sets whether the decor view should fit root-level content views for
    3. * {@link WindowInsetsCompat}.
    4. *

    5. * If set to {@code false}, the framework will not fit the content view to the insets and will
    6. * just pass through the {@link WindowInsetsCompat} to the content view.
    7. *

    8. *

    9. * Please note: using the {@link View#setSystemUiVisibility(int)} API in your app can
    10. * conflict with this method. Please discontinue use of {@link View#setSystemUiVisibility(int)}.
    11. *

    12. *
    13. * @param window The current window.
    14. * @param decorFitsSystemWindows Whether the decor view should fit root-level content views for
    15. * insets.
    16. */
    17. public static void setDecorFitsSystemWindows(@NonNull Window window,
    18. final boolean decorFitsSystemWindows) {
    19. if (Build.VERSION.SDK_INT >= 30) {
    20. Api30Impl.setDecorFitsSystemWindows(window, decorFitsSystemWindows);
    21. } else if (Build.VERSION.SDK_INT >= 16) {
    22. Api16Impl.setDecorFitsSystemWindows(window, decorFitsSystemWindows);
    23. }
    24. }

    可以看到函数内部已经做了版本适配

    效果

    分别是有导航栏和无导航栏的沉浸式效果

  • 相关阅读:
    软件测试(二)用例
    SpringBoot实践(三十):简单分析SpringBoot自动配置和启动
    【VUE】
    【Flutter】设计原则(2)深入解析 SOLID 原则的应用
    keepalived实现nginx高可用
    初识python
    234.回文链表
    [vite.js]按需加载自动注册组件
    iNFTnews | 科技巨头加快进军Web3和元宇宙
    【SQL之降龙十八掌】01——亢龙有悔:入门10题
  • 原文地址:https://blog.csdn.net/somethingstill/article/details/136165834