ListView 在横向模式下不显示所有项目

ListView not displaying all items in Landscape mode

我有一个 Activity 在纵向模式下适合屏幕尺寸,如下图所示

问题 是当我旋转屏幕时,ListView 折叠并仅显示一项,如下图所示

什么 导致了这个问题,我该如何解决

这是我的 xml 布局文件

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:orientation="vertical">

        <TextView
            android:id="@+id/randomWordLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="dfdfdf"
            android:textColor="@android:color/white"
            android:textSize="50sp"
            android:padding="10dp"
            android:layout_gravity="center_horizontal"/>

        <android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context="com.example.yousaf.mcqstest.GamePlay"
            android:id="@+id/listViewContainer">

            <ListView
                android:id="@+id/myListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:background="#fff"
                android:dividerHeight="7dp"
                android:padding="10dp">
            </ListView>

        </android.support.constraint.ConstraintLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:paddingTop="15dp">

            <TextView
                android:id="@+id/textPoints"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="Points : "
                android:textSize="22sp"
                android:textColor="#fff"
                android:layout_gravity="center"
                android:gravity="center"/>

            <TextView
                android:id="@+id/textPointsValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="22sp"
                android:textColor="#fff"
                android:layout_gravity="center" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:paddingTop="15dp">

            <TextView
                android:id="@+id/textCorrectAnswers"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="Correct Answers : "
                android:textSize="22sp"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/textCorrectAnswersValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="22sp"
                android:textColor="#fff" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:paddingTop="20dp">

            <TextView
                android:id="@+id/textWrongAnswers"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="Wrong Answers   : "
                android:textSize="22sp"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/textWrongAnswersValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="22sp"
                android:textColor="#fff" />

        </LinearLayout>

        <Button
            android:id="@+id/btnStopGame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:padding="15dp"
            android:background="#222"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>

</ScrollView>

因为列表视图的高度是换行内容。

请移除 ConstraintLayout

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context="com.example.yousaf.mcqstest.GamePlay"
            android:id="@+id/listViewContainer"></ConstraintLayout>

我希望这会奏效..

所以我通过

解决了问题中提到的问题

1- 横向模式的单独布局文件。在res文件夹中新建一个名为layout-land的文件夹,然后将我问题中包含的布局文件复制到layout-land文件夹中,然后根据横向纵向进行适当的更改。

2- 固定高度 ListView。在我的例子中,我将 ListView 的高度设置为 190dp.

现在我的横向布局如下图所示