如何在ScrollView中将LinearLayout的高度设置为match parent?

3
以下是我的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>
3个回答

11

试试这个,我认为它可能会对你有帮助。

<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>

4

这个LinearLayout应该有android:layout_height="wrap_content"

原因在于,如果scrollview的子项和scrollview本身大小相同(高度都是match_parent),那么就没有需要滚动显示的内容了,因为它们大小相同,而scrollview的高度只能等于屏幕高度。

如果LinearLayout的高度为wrap_content,则其高度与屏幕高度无关,并且scrollview将能够滚动显示其中的内容。

请记住,一个scrollview只能有一个直接子项,而且该子项需要android:layout_height="wrap_content"


0

在 ScrollView 中定义一个高度为 match parent 的 LinearLayout 是没有意义的。您必须将 LinearLayout 的高度设置为 wrap content 才能滚动它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接