【问题标题】:CollapsingToolbarLayout Showing padding bottom in ImageViewCollapsingToolbarLayout 在 ImageView 中显示填充底部
【发布时间】:2021-08-21 00:42:11
【问题描述】:

当在 CollapsingToolbarLayout 中将 android:fitsSystemWindows="true" 和 android:adjustViewBounds="true" 一起用于 ImageView 时,它会在底部为 ImageView 提供填充

我发布它的答案来帮助这个参考这个问题解决方案来帮助其他人解决我很久以前提出的 github 问题

预期行为:

当前行为:

布局 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>

【问题讨论】:

    标签: android material-design material-components-android material-components


    【解决方案1】:

    要解决这个问题,您需要将 android:fitsSystemWindows="true" 从根添加到每个子视图,直到您想要在状态栏下方显示的视图,然后将 android:fitsSystemWindows="false" 设置为您想要的那些视图根据状态栏高度获得自动填充,因此这将解决图像视图上的填充问题,但现在黑色渐变行为怪异并且要修复工具栏的大小,以修复我已使用窗口插入检查在代码中手动将填充设置为渐变下面的代码

    布局

    <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"
            android:fitsSystemWindows="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <include
                android:id="@+id/base_model_detail_layout_include"
                layout="@layout/base_model_detail_layout_include" />
    
        </androidx.core.widget.NestedScrollView>
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:minHeight="?actionBarSize"
                app:contentScrim="?colorPrimarySurface"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:statusBarScrim="?colorPrimarySurface"
                app:titleEnabled="false">
    
                <ImageView
                    android:id="@+id/imageView_bundleOffer"
                    style="@style/Widget.PNBO.BundleOfferDetailFragment.ImageView"
                    android:fitsSystemWindows="true"
                    android:visibility="gone"
                    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="?actionBarSize"
                    android:background="@drawable/gradient_black_transparent"
                    android:fitsSystemWindows="true"
                    android:visibility="gone"
                    app:layout_collapseMode="pin"
                    tools:visibility="visible" />
    
                <com.google.android.material.appbar.MaterialToolbar
                    android:id="@+id/toolbar"
                    android:fitsSystemWindows="false"
                    style="@style/Widget.PNBO.BundleOfferDetailFragment.Toolbar" />
    
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    Kotlin 代码

        private fun setUpSystemBars() {
    
            binding.root.apply {
    
                ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
    
                    val top = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top
                    val height: Int
    
                    binding.viewImageUpperGradient?.also {
                        height = it.layoutParams.height
                        it.layoutParams.height = height + top
                    }
    
                    insets
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2014-08-28
      • 2015-03-20
      • 1970-01-01
      • 2013-07-28
      • 2011-11-02
      • 2018-06-19
      相关资源
      最近更新 更多