【问题标题】:CollapsingToolbarLayout and blank space in NestedScrollViewNestedScrollView 中的 CollapsingToolbarLayout 和空白区域
【发布时间】:2016-11-08 20:47:10
【问题描述】:

CollapsingToolbarLayout 有问题。我不想将NestedScrollView 滚动到最后,因为如果内容(在我的情况下为文本)很短,它会留下很多空白。以下是截图:

还有:

这是 XML:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/coordinator"
   >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_description"
        >



        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">

             <android.support.v4.view.ViewPager
                    android:id="@+id/image_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/dots_holder" />
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="15dp"
                    android:id="@+id/dots_holder"
                    android:paddingTop="2dp"
                    android:paddingBottom="2dp"
                    android:gravity="center"
                    android:layout_alignParentBottom="true"
                    android:visibility="gone" />

            </RelativeLayout>


        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>



    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:padding="8dp"
        android:id="@+id/nested_scrollView">

      <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/name"
                android:textSize="@dimen/abc_text_size_headline_material"
                android:textColor="@color/abc_primary_text_material_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/othernames"
                android:layout_below="@+id/name"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/secondary_text_default_material_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="first"
                android:id="@+id/season_head"
                android:layout_alignParentBottom="false"
                android:layout_alignParentLeft="false"
                android:layout_below="@+id/othernames"
                android:layout_marginTop="12dp"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/abc_primary_text_material_light" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="second"
                android:id="@+id/places_head"
                android:layout_marginTop="12dp"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/abc_primary_text_material_light"
                android:layout_below="@+id/season" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/season"
                android:layout_below="@+id/season_head"
                android:layout_marginTop="@dimen/desc_clauses_margin"
                android:textSize="@dimen/abc_text_size_body_1_material"
                android:textIsSelectable="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/places"
                android:layout_below="@+id/places_head"
                android:textSize="@dimen/abc_text_size_body_1_material"
                android:layout_marginTop="@dimen/desc_clauses_margin" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="third"
                android:id="@+id/description_head"
                android:textSize="@dimen/abc_text_size_subhead_material"
                android:textColor="@color/abc_primary_text_material_light"
                android:layout_below="@+id/places"
                android:layout_marginTop="12dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXT"
                android:id="@+id/description"
                android:layout_below="@+id/description_head"
                android:textSize="@dimen/abc_text_size_body_1_material"
                android:layout_marginTop="@dimen/desc_clauses_margin" />
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

我想在内容结束时停止滚动。

【问题讨论】:

  • 问题是当您使用AppBarLayoutappbar_scrolling_view_behavior 时,AppBarLayout(在您的情况下为NestedScrollView)下方的视图的大小就像应用栏已经 滚动出视图,然后定位在应用栏下方切断底部。因此,在初始滚动时,您可能会认为某些东西实际上正在滚动,但整个滚动视图只是向上移动,而应用栏移开。因此,实现您的结果需要一些棘手的代码来测量内容并根据其大小禁用滚动行为。对不起。
  • 很遗憾,我想不出 CollapsingToolbarLayout 的解决方案,但我找到了一个简单的视差效果解决方案,就像折叠工具栏一样,这里是解决方案:stackoverflow.com/a/34421687/6475516

标签: android android-layout android-collapsingtoolbarlayout android-nestedscrollview


【解决方案1】:

回复这个问题有点晚了,但我认为还有其他开发人员有类似的问题,因此现在回复它。要了解为什么会发生这种情况,我们必须了解以下核心概念:

  • CoordinatorLayout 实际上是一个 FrameLayout ,带有一些自定义更改以提供我们看到的行为。因此,当我们将AppBarLayoutNestedScrollView 用作子代时,它们本质上是相互重叠的。 CoordinatorLayout 获取AppBarLayout 的高度,并确保 NestedScroll 视图显示在 AppBarLayout 下方(如果是正常的 FrameLayout,它将根据顺序显示在 AppBar 下方或上方)。这就是为什么我们看到的屏幕就像上面的屏幕截图 1。

  • 但是为什么是空格呢?当您向上滚动时,CoordinatorLayout 正在向上滚动 AppBarLayout 并折叠 CollapsingToolBar,但也会向上移动 NestedScrollView 以提供滚动行为。当NestedScrollView 到达顶部时,现在屏幕有整个视图来显示内容。由于我们的NestedScrollViewwrap_content,它实际上只占用了显示上下文(TextView)所需的空间,其余区域是应用程序/活动的默认背景。我们的活动和NestedScrollView 背景是相同的,因此它给人的感觉是你的NestedScrollView 占据了整个空间,但实际上并没有。尝试为NestedScrollView设置不同的颜色背景,会更有意义。

  • 如果你真的想实现上述行为,一种方法是避免NestedScrollView 并将RelativeLayout 拉出并使其成为外部CoordinatorLayout 的子级,它将显示预期的结果。这样做的副作用是您不会在TextView 内容上获得滚动效果,但工具栏会起作用。如果你想要滚动行为,你必须使用 NestedScrollView,即使没有Coordinator,它的行为也是一样的,因为没有太多东西可以覆盖整个屏幕并且应该看到空白区域。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 2015-09-08
    • 1970-01-01
    • 2019-04-22
    • 2023-03-15
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    相关资源
    最近更新 更多