如何在ScrollView中修复ListView?

时间:2019-10-25 01:23:53

标签: android xml layout interface

我需要在ScrollBar中放置一个ListView。我知道关于此主题还有其他问题,但是答案非常复杂且糟糕。

ListView不会显示所有元素,仅显示第一个元素。

相关代码的主要结构:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                ....MORE ELEMENTS....
                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    ></ListView>

            </LinearLayout>
        </ScrollView>
    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

ListView放在单独的LinearLayout父级中,也可以根据您的选择将其添加到单独的.xml文件中,然后添加

 android:fillViewport="true"

在您的滚动视图中,就像这样

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!....MORE ELEMENTS....>

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

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

        </LinearLayout>

    </LinearLayout>

</ScrollView>