【问题标题】:NestedScrollView content is smaller than screen heightNestedScrollView 内容小于屏幕高度
【发布时间】:2018-11-10 14:55:05
【问题描述】:

我知道这个问题经常被问到,但在我的情况下我找不到正确的方法。

正如您在我的 xml 文件中看到的,我正在使用 CoordinatorLayout、NestedScrollView 和 AppBarLayout。当nestedScrollview 的内容小于屏幕高度时,空出多余的空白。

我怎样才能删除这个多余的空白?

谢谢大家

 <?xml version="1.0" encoding="utf-8"?>
    <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.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#f000f0"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hello World!" />

                <!-- more content but not enought to fill screen -->

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.AppBarLayout
            android:id="@+id/main_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:background="#ff0000">

                        <TextView
                            android:id="@+id/main_linearlayout_title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="30dp"
                            android:layout_marginTop="30dp"
                            android:text="PSEUDO"
                            android:textColor="#ffffff"
                            android:textStyle="bold" />
                    </LinearLayout>


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

        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimaryDark">

<!-- some content (title) -->
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CoordinatorLayout>

编辑 1

  • 没有滚动

  • 使用 NestedScrollView 高度 wrap_content

  • 使用 NestedScrollView 高度 match_parent(所以它是同样的问题,而不是有空白空间,它是粉红色的,但它剩下一个额外的无用空间)

所以要明确一点,我在最后一个“Hello World”下什么都不想,并且不要滚动超过必要的范围。

【问题讨论】:

  • 你想达到什么目的?
  • @MarkBuikema 我不想要这个额外的空间并在你好世界的尽头完成视图
  • 我不明白你想要达到什么目的。您希望空白空间用于什么?
  • @MarkBuikema 我不想滚动超过我的内容并且看不到这个空白空间。

标签: android android-coordinatorlayout android-nestedscrollview


【解决方案1】:

NestedScrollView 的高度设置为match_parent

【讨论】:

    【解决方案2】:

    将您的 NestedScrollView 的高度设置为 match_parent。将 LinearLayout 的高度设置为 match_parent。修改你的 TextView

     <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="hello world"/>
    

    为每个 TextView 分配 0dp 的高度和 1 的权重。layout_weight 指定布局中要分配给 View 的额外空间的多少。这是做什么的将您的 TextView 平均扩展到剩余空间(在这种情况下,它恰好是整个屏幕)。
    Explanation in the official developers doc.

    【讨论】:

    • 我和嵌套滚动视图高度 match_parent 一样。我不想滚动超过必要的范围
    • 如果你不想这样,那么使用滚动视图高度作为 wrap_content
    • 原来是我的问题
    • 你说你不想要那个额外的空白空间,但我的解决方案不是填满你的屏幕并删除所有空白空间吗?
    • 我说我根本不想要这个额外的空间,空白或不一样,这是一个无用的空间和用户滚动
    猜你喜欢
    • 2014-09-19
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多