如何解决安卓开启全屏模式时导航栏残留永久空白无法隐藏的问题
解决方案
- 修正布局属性配置
你的根布局RelativeLayout和AppBarLayout都设置了android:fitsSystemWindows="true",该属性会默认给系统状态栏、导航栏预留内边距,这是导航栏位置出现空白的核心原因。同时需要给AppBarLayout添加id,给PlayerView添加非全屏时的位置约束,修改后的布局核心代码如下:
<!-- 给AppBarLayout添加id --> <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" app:elevation="0dp"> <!-- Toolbar代码保持不变 --> </com.google.android.material.appbar.AppBarLayout> <!-- 给PlayerView添加相对位置约束 --> <com.google.android.exoplayer2.ui.PlayerView android:id="@+id/exoplayer_video" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/app_bar_layout" android:background="@color/black" app:controller_layout_id="@layout/custom_controller" app:resize_mode="fit" app:player_layout_id="@layout/exo_simple_player_view" app:use_controller="true"/>
- 补充全屏切换逻辑
在Activity中添加全屏切换代码,切换全屏时动态修改布局可见性和fitsSystemWindows属性,使用官方兼容包API适配全版本Android系统,Kotlin示例代码如下:
// 启用全屏 private fun enterFullScreen() { // 让DecorView不占用系统栏空间 WindowCompat.setDecorFitsSystemWindows(window, false) WindowInsetsControllerCompat(window, window.decorView).apply { // 隐藏状态栏和导航栏 hide(WindowInsetsCompat.Type.systemBars()) // 侧滑边缘可临时显示系统栏 systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE } // 隐藏顶部Toolbar容器 binding.appBarLayout.visibility = View.GONE // 取消根布局的系统栏预留空间 binding.playerLayout.fitsSystemWindows = false } // 退出全屏 private fun exitFullScreen() { WindowCompat.setDecorFitsSystemWindows(window, true) WindowInsetsControllerCompat(window, window.decorView).show(WindowInsetsCompat.Type.systemBars()) binding.appBarLayout.visibility = View.VISIBLE binding.playerLayout.fitsSystemWindows = true }
如果使用Java开发,对应API逻辑一致,调用对应类的方法即可。
- 优化主题配置
将styles.xml中主题的父类替换为NoActionBar类型,避免ActionBar残留配置的影响,同时添加透明状态栏配置,修改后的样式代码如下:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="colorPrimary">@color/purple_700</item> <item name="colorPrimaryDark">@color/black</item> <item name="colorAccent">#F87A8D</item> <item name="windowActionModeOverlay">true</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:navigationBarColor">@android:color/transparent</item> <!-- 新增透明状态栏配置 --> <item name="android:statusBarColor">@android:color/transparent</item> <item name="android:windowTranslucentNavigation">true</item> </style>
内容的提问来源于stack exchange,提问作者user16566034
相关产品推荐
相关产品推荐

