NestedScrollView无法使用match_parent height子进行滚动

Nor*_*tan 16 android android-nestedscrollview

我实现NonSwipeableViewPager与片段有这样的NestedScrollView,我期望的是scrollview可以向上滚动并显示2个textviews:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

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

            <include
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                layout="@layout/header" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"
                android:src="@drawable/ic_up" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

但它无法滚动,我尝试了很多方法,但仍然没有得到任何解决方案

Tim*_*Tim 78

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
Run Code Online (Sandbox Code Playgroud)

这个线性布局应该有android:layout_height="wrap_content".

原因是如果scrollview的子节点与scrollview本身的尺寸相同(两者都是match_parent高度),则意味着没有任何内容可以滚动,因为它们具有相同的大小,并且scrollview只会与屏幕一样高.

如果linearlayout的高度为高度,wrap_content则高度与屏幕的高度无关,并且scrollview将能够滚动它.

请记住,一个scrollview只能有一个直接的孩子,而那个孩子需要 android:layout_height="wrap_content"

  • 对我来说,只有`fillViewPort = true`被唤醒.在我的情况下,孩子的身高并不重要 (8认同)

Tar*_*qul 13

在我的情况下, app:layout_behavior="@string/appbar_scrolling_view_behavior"这只有在某个人面临的问题会尝试并且也可能解决您的问题时才有效。你也应该添加android:fillViewport="true"但没有这个我的代码工作。

 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/subscription_background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
Run Code Online (Sandbox Code Playgroud)

  • 我认为 app:layout_behavior 行为仅在我们使用 coordinatorlayout 时才适用。 (8认同)

小智 5

对我来说,当我在 androidx.core.widget.NestedScrollView 中为最后一个孩子添加“android:layout_marginBottom="100dp"”时,它就起作用了