You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中如何用Java实现状态栏与底部导航栏透明

解决Activity全屏+透明状态栏/导航栏后上下空白问题

一、通过主题配置基础样式

在res/values/styles.xml(或对应版本的values文件夹)中定义主题:

<style name="FullScreenTransparentTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- 隐藏ActionBar -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <!-- 透明状态栏 -->
    <item name="android:statusBarColor">@android:color/transparent</item>
    <!-- 透明导航栏 -->
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <!-- 允许布局延伸到状态栏和导航栏下方 -->
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

然后在AndroidManifest.xml中给目标Activity设置这个主题:

<activity
    android:name=".YourActivity"
    android:theme="@style/FullScreenTransparentTheme"/>

二、在Activity代码中补充配置

在Activity的onCreate方法中,添加以下代码(需在setContentView之前调用):

Kotlin示例

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    window.apply {
        // 设置全屏标记,允许布局延伸到系统栏区域
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        // 设置透明状态栏和导航栏
        statusBarColor = Color.TRANSPARENT
        navigationBarColor = Color.TRANSPARENT
    }
    setContentView(R.layout.activity_your)
}

Java示例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window window = getWindow();
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    window.setStatusBarColor(Color.TRANSPARENT);
    window.setNavigationBarColor(Color.TRANSPARENT);
    setContentView(R.layout.activity_your);
}

三、布局文件适配

核心:去掉根布局的fitsSystemWindows="true"属性,这个属性会强制布局避开系统栏区域,直接导致上下空白。

错误示例(会出现空白):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

正确示例:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

如果需要让某个子View(比如标题栏)避开状态栏,可以给该View设置动态padding:

  1. 在res/values/dimens.xml中定义状态栏高度:
<dimen name="status_bar_height">24dp</dimen>
  1. 或者通过代码获取状态栏真实高度后设置padding:
// Kotlin获取状态栏高度
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
val statusBarHeight = if (resourceId > 0) resources.getDimensionPixelSize(resourceId) else 0
yourTitleView.setPadding(0, statusBarHeight, 0, 0)

四、Android 11及以上额外适配

在主题中添加以下属性,避免导航栏手势模式下出现异常:

<item name="android:enforceNavigationBarContrast">false</item>

内容的提问来源于stack exchange,提问作者Rakesh Saini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 10:55:14