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

如何让CollapsingToolbarLayout折叠时状态栏保持隐藏?

问题描述

我在布局中使用CollapsingToolbarLayout展示图片,为使图片延伸至页面顶部设置了隐藏状态栏。展开状态下设置正常生效:
CollapsingToolbarLayout展开时的状态栏
但向下滚动折叠CollapsingToolbarLayout后,状态栏会突然显示:
CollapsingToolbarLayout折叠时的状态栏

我的布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="show"
            type="fr.steph.showmemories.models.ShowModel" />
    </data>
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_bar_height"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@id/details_toolbar">
                <ImageView
                    android:id="@+id/details_show_image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:fitsSystemWindows="true"
                    app:context="@{context}"
                    app:error="@{@drawable/ic_default_image}"
                    app:imageUrl="@{show.imageUrl}"
                    app:layout_collapseMode="parallax"/>
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/details_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:title="@{show.name}"/>
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>
        <androidx.core.widget.NestedScrollView
            android:id="@+id/details_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/default_margin"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <!-- Page content -->
        </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

如何实现CollapsingToolbarLayout折叠时状态栏仍保持隐藏?


解决方案

1. 给CollapsingToolbarLayout添加fitsSystemWindows属性

确保CollapsingToolbarLayout始终适配系统窗口,避免折叠时状态栏重新出现:

<com.google.android.material.appbar.CollapsingToolbarLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true" <!-- 添加此行 -->
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    app:toolbarId="@id/details_toolbar">

2. 配置主题实现状态栏透明

在styles.xml中创建或修改页面主题,设置状态栏透明:

<style name="FullScreenDetailsTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <!-- 适配Android 11+刘海屏 -->
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

然后在AndroidManifest.xml中为对应Activity绑定该主题:

<activity
    android:name=".YourDetailsActivity"
    android:theme="@style/FullScreenDetailsTheme"/>

3. 代码中强制保持全屏模式

在Activity的onCreate方法中添加代码,固定窗口全屏状态:

Java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置系统UI标志,保持全屏布局
    getWindow().getDecorView().setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_FULLSCREEN
    );
    // 设置状态栏透明
    getWindow().setStatusBarColor(Color.TRANSPARENT);
    setContentView(R.layout.your_details_layout);
    // 其他初始化逻辑
}

Kotlin:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    window.decorView.systemUiVisibility = (
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        or View.SYSTEM_UI_FLAG_FULLSCREEN
    )
    window.statusBarColor = Color.TRANSPARENT
    setContentView(R.layout.your_details_layout)
    // 其他初始化逻辑
}

4. 移除AppBarLayout的fitsSystemWindows属性(可选)

如果存在多层适配冲突,尝试移除AppBarLayout的android:fitsSystemWindows="true"属性:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    <!-- 移除此行 android:fitsSystemWindows="true" -->
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 08:15:49