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

CollapsingToolbarLayout中ImageView同时开启fitsSystemWindows和adjustViewBounds为何底部出现内边距

CollapsingToolbarLayout中ImageView同时设置fitsSystemWindows和adjustViewBounds出现底部多余内边距的解决方案

当在CollapsingToolbarLayout内部的ImageView上同时设置android:fitsSystemWindows="true"和android:adjustViewBounds="true"属性时,ImageView底部会出现多余的内边距,以下是实测可用的解决方案。

效果对比

预期效果

预期效果示意图

实际异常表现

实际异常表现示意图

复现代码

布局XML代码

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include
        android:id="@+id/include_bundle_detail_layout"
        layout="@layout/include_bundle_detail_layout" />

</androidx.core.widget.NestedScrollView>

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    style="@style/Widget.Base.AppBarLayout"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimarySurface"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">

        <ImageView
            android:id="@+id/imageView_bundleOfferImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:visibility="gone"
            app:layout_collapseMode="parallax"
            tools:ignore="ContentDescription"
            tools:src="@drawable/playstore_feature_graphic"
            tools:visibility="visible" />

        <View
            android:id="@+id/view_image_upper_gradient"
            android:layout_width="match_parent"
            android:layout_height="96dp"
            android:background="@drawable/gradient_black_transparent"
            android:visibility="gone"
            app:layout_collapseMode="pin"
            tools:visibility="visible" />

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/Widget.Base.Toolbar"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />

    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

styles.xml代码

<style name="Widget.Base.Toolbar">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">?actionBarSize</item>
    <item name="android:paddingTop">24dp</item>
</style>

解决方案

可根据业务场景选择任意一种方案:

  • 方案1:移除ImageView的android:adjustViewBounds="true"属性。当前代码已经为ImageView设置了android:scaleType="centerCrop",该属性会自动按比例裁剪图片填充整个View区域,不需要额外通过adjustViewBounds调整尺寸,移除后底部多余内边距会直接消失。
  • 方案2:如果业务必须保留adjustViewBounds属性,那么移除ImageView的android:fitsSystemWindows="true"属性即可,fitsSystemWindows属性只需要保留在父级的CoordinatorLayout、AppBarLayout和CollapsingToolbarLayout上,就能正常适配状态栏,不会出现底部留白问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 17:36:08