【Android】状态栏、导航栏、沉浸式的展示与交互处理

废话少说上代码

  // 默认设置
  val controler = WindowCompat.getInsetsController(window, window.decorView)
  controler.show(WindowInsetsCompat.Type.systemBars()) // 展示系统view:状态栏+导航栏+系统标题栏
  WindowCompat.setDecorFitsSystemWindows(window, true) // 页面布局是否在状态栏下方,false:侵入状态栏
  controler.isAppearanceLightStatusBars = true // 状态栏字体明暗主题,true为黑色,false为白色
  window.statusBarColor = Color.WHITE // 状态栏背景色
  
  // 侵入状态栏
  val controler = WindowCompat.getInsetsController(window, window.decorView)
  controler.show(WindowInsetsCompat.Type.systemBars()) // 展示系统view:状态栏+导航栏+系统标题栏
  WindowCompat.setDecorFitsSystemWindows(window, false) // 页面布局是否在状态栏下方,false:侵入状态栏
  controler.isAppearanceLightStatusBars = true // 状态栏字体明暗主题,true为黑色,false为白色
  window.statusBarColor = Color.TRANSPARENT // 状态栏背景色,侵入时最好设置为透明

  // 沉浸式
  val controler = WindowCompat.getInsetsController(window, window.decorView)
  controler.hide(WindowInsetsCompat.Type.systemBars()) // 隐藏系统view:状态栏+导航栏+系统标题栏
  // 设置系统栏交互方式为从边缘滑出展示,搭配上一行代码使用
  controler.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
  WindowCompat.setDecorFitsSystemWindows(window, false) // 页面布局是否在状态栏下方,false:侵入状态栏
  window.statusBarColor = Color.TRANSPARENT // 状态栏背景色,侵入时最好设置为透明
  window.let {
      // 设置侵入挖孔屏,否则系统将会留出挖孔区域,该区域通常为黑色
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
          it.attributes.layoutInDisplayCutoutMode =
              WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                  it.attributes = it.attributes
      }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 沉浸状态栏指的是在应用中隐藏系统状态栏,使应用的界面能够占据整个屏幕空间,提供更加沉浸的使用体验。在 Android 4.4 KitKat(API 级别 19)及以上版本中,引入了沉浸状态栏的支持。 要实现沉浸状态栏,可以按照以下步骤进行操作: 1. 在 AndroidManifest.xml 文件中,为对应的 Activity 设置 `android:theme` 属性为 `@style/Theme.AppCompat.NoActionBar` 或者其他无 ActionBar 特性的主题。 2. 在对应 Activity 的 `onCreate` 方法中添加以下代码来隐藏系统状态栏: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } ``` 3. 如果你想要在状态栏下方留出一定的空间,可以设置 paddingTop,例如: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int statusBarHeight = getStatusBarHeight(); View view = findViewById(R.id.your_view_id); view.setPadding(0, statusBarHeight, 0, 0); } private int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } ``` 这样就可以实现 Android 沉浸状态栏的效果。需要注意的是,沉浸状态栏可能会导致一些 UI 布局的问题,需要根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值