Android 12实现仅隐藏/显示状态栏且内容全屏(无废弃代码)
适配API 23-31的全屏状态栏切换方案(兼容刘海屏)
需求说明
实现类似WEBTOONS应用的交互效果:内容全屏时隐藏状态栏,退出全屏时显示状态栏;适配API Level 23至31版本,全程不使用废弃API。
问题回顾
此前尝试的FLAG_FULLSCREEN、FLAG_LAYOUT_NO_LIMITS、systemUiVisibility等方法,要么存在API废弃问题,要么在刘海屏设备上出现全屏状态下状态栏区域黑块、退出全屏后内容被状态栏遮挡的异常。
解决方案
核心思路
同时处理系统栏显示隐藏与内容区域适配两个维度:通过控制内容是否延伸至系统栏区域,解决刘海屏黑块和内容遮挡问题;针对不同API版本使用对应非废弃API,保证兼容性。
1. 封装全屏切换工具类
统一处理版本兼容逻辑,避免重复代码:
import android.app.Activity import android.os.Build import android.view.View import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat object FullScreenHelper { // 进入全屏(隐藏状态栏) fun enterFullScreen(activity: Activity) { val window = activity.window // 允许内容延伸到系统栏区域,解决刘海屏黑块问题 WindowCompat.setDecorFitsSystemWindows(window, false) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // API 30+ 使用WindowInsetsController(非废弃API) window.insetsController?.apply { hide(WindowInsetsCompat.Type.systemBars()) // 设置触摸边缘弹出系统栏后自动隐藏,匹配WEBTOONS交互 systemBarsBehavior = android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE } } else { // API 23-29 使用systemUiVisibility(该版本区间内未废弃) @Suppress("DEPRECATION") window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE) } } // 退出全屏(显示状态栏) fun exitFullScreen(activity: Activity) { val window = activity.window // 恢复内容区域适配,避免被状态栏遮挡 WindowCompat.setDecorFitsSystemWindows(window, true) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { window.insetsController?.show(WindowInsetsCompat.Type.systemBars()) } else { @Suppress("DEPRECATION") window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE } } }
2. 刘海屏适配补充配置
在AndroidManifest.xml中添加刘海屏适配元数据,确保内容可以覆盖刘海区域:
<application ...> <activity android:name=".YourFullScreenActivity" android:theme="@style/Theme.YourApp.FullScreen"> <!-- 适配Android P及以上刘海屏 --> <meta-data android:name="android.max_aspect" android:value="2.4" /> <meta-data android:name="android.notch_support" android:value="true" /> </activity> </application>
同时配置全屏主题样式,确保系统栏透明:
<style name="Theme.YourApp.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> <item name="android:navigationBarColor">@android:color/transparent</item> </style>
3. 调用方式
在Activity中直接调用工具类方法即可完成状态切换:
// 进入全屏模式 FullScreenHelper.enterFullScreen(this) // 退出全屏模式 FullScreenHelper.exitFullScreen(this)
关键细节说明
WindowCompat.setDecorFitsSystemWindows:核心适配方法,设置为false时内容延伸至系统栏区域,解决刘海屏黑块;设置为true时自动给内容添加系统栏高度的padding,避免内容被遮挡。- 版本兼容逻辑:API30+采用官方推荐的
WindowInsetsController,低版本使用systemUiVisibility(该API在23-29版本区间内未被标记为废弃,可安全使用)。 - 沉浸式交互:通过
IMMERSIVE_STICKY(低版本)和BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE(高版本)实现触摸边缘弹出系统栏后自动隐藏的效果,匹配WEBTOONS的交互体验。
内容的提问来源于stack exchange,提问作者rkr33
相关产品推荐
相关产品推荐

