当前位置:   article > 正文

Android 沉浸式状态栏

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. * <p>
  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. * </p>
  8. * <p>
  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. * </p>
  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. }

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

效果

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/119181
推荐阅读
相关标签
  

闽ICP备14008679号