ScrollView根本不滚动

tho*_*aus 24 android scroll android-linearlayout android-scrollview

我无法正确滚动ScrollView.它总是切断底部的内容,就像它是正常的LinearLayout一样.

我的代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout android:id="@+id/scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="true"
        android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)

当然,我已经尝试添加/删除"fillViewport"和"isScrollContainer"属性,它根本没有改变任何东西.

提前致谢.

tho*_*aus 26

答案:当用作XML布局的根元素时,ScrollView无法正常工作.它必须包含在LinearLayout中.

解决方案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <LinearLayout android:id="@+id/scroll_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 这不是真的,您不能使用“ScrollView”作为根元素,您可以使用“ScrollView”作为“XML”的根元素,将其用作根元素没有问题。 (3认同)
  • 谢谢!无论 Android 多么愚蠢,我都不会想到这也是原因之一-_- (2认同)

小智 8

? 尝试了上述所有答案,但我的问题仍未解决。经过一些调试和更改后,我的问题得到了解决,即 ScrollView 正在滚动。

  • 我们不需要向 ScrollView 添加任何父元素以使其滚动(如此处的其他一些答案中所述)。

??修复是针对我的问题?

  • 将 ScrollView 的高度更改为 0dp

    android:layout_height="0dp"

  • 将顶部和底部约束到父/各自的视图/元素

    app:layout_constraintTop_toBottomOf="@+id/imageView4" app:layout_constraintBottom_toBottomOf="parent"

最终的 xml 看起来像这样

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/imageView4"
    app:layout_constraintBottom_toBottomOf="parent">

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

           <!-- Loong content -->

    </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)


Var*_*ran 6

Scroll View as Parent 没有问题。当我们向滚动视图的直接子级添加填充/边距时,我遇到了这样的问题。保持滚动视图的子级只有高度和宽度属性,它会正常工作。

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:fillViewport="true">

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

  • 天哪,我已经为此苦苦挣扎了大约一个小时。这是金子。 (2认同)
  • 也为我工作!谢谢你! (2认同)