如何在 ScrollView 中将 LinearLayout 高度设置为匹配父级

How can I set LinearLayout height as match parent within ScrollView

下面是我的 .XML 代码。我能有办法解决这个问题吗?

<ScrollView 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:id="@+id/ticket_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="true">

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</ScrollView>

在ScrollView中定义匹配父高度的LinearLayout是没有意义的。您必须设置 LinearLayout 高度来包裹内容才能滚动它。

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

原因是,如果滚动视图的子视图与滚动视图本身的大小相同(高度均为 match_parent),则意味着没有可滚动的内容,因为它们的大小相同且scrollview 只会和屏幕一样高。

如果 LinearLayout 的高度为 wrap_content,则该高度与屏幕高度无关,scrollview 将能够滚动它。

请记住,一个滚动视图只能有一个直接子视图,并且该子视图需要 android:layout_height="wrap_content"

试试这个,我想它可能对你有帮助。

<ScrollView 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:id="@+id/ticket_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="true">

        <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    </ScrollView>