如何在 ScrollView 中使用连续的 RelativeLayouts?

How to use successive RelativeLayouts in a ScrollView?

我被要求提供一个带有滚动视图的视图,以便进一步添加可用的按钮。问题是为了将按钮排列在 3 x 3 行中,我使用了三个连续的 RelativeLayouts,如下面的代码所示

<View
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:background="@color/dialogWhite"
    android:layout_marginTop="5dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/symptomsTxt"/>

<TextView
    android:id="@+id/hdrText"
    android:layout_width="370dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/todays_side_effects"
    android:textAlignment="center"
    android:textSize="16sp"
    app:layout_constraintTop_toBottomOf="@+id/symptomsTxt"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<ScrollView
    android:id="@+id/scrollViewWB"
    android:layout_width="fill_parent"
    android:layout_height="400dp"
    android:visibility="visible"
    android:layout_marginTop="5dp"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/hdrText">

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

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/buttonView"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal"
            tools:ignore="NotSibling">

            <Button
                android:id="@+id/buttonSel1"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_diarrhoeia_selected"
                android:onClick="diarreaClick"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent" />

            <TextView
                android:id="@+id/textViewLoc"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="45dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/diarrea"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel1" />

            <Button
                android:id="@+id/buttonSel2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="160dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_vomiting_selected"
                android:onClick="vomittingClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel1" />

            <TextView
                android:id="@+id/textViewLoc2"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="145dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/vomiting"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel2" />

            <Button
                android:id="@+id/buttonSel3"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="260dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_headache_selected"
                android:onClick="headacheClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel2" />

            <TextView
                android:id="@+id/textViewLoc3"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="245dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/headache"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc2"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel3" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/buttonView2"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginTop="15dp"
            android:orientation="horizontal"
            tools:ignore="NotSibling">

            <Button
                android:id="@+id/buttonSel4"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_weakness_selected"
                android:onClick="weaknessClick"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent" />

            <TextView
                android:id="@+id/textViewLoc4"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="45dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/weakness"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel4" />

            <Button
                android:id="@+id/buttonSel5"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="160dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_sweating_selected"
                android:onClick="sweatingClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel4" />

            <TextView
                android:id="@+id/textViewLoc5"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="145dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/sweating"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc4"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel5" />

            <Button
                android:id="@+id/buttonSel6"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="260dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_tremors_selected"
                android:onClick="tremorsClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel5" />

            <TextView
                android:id="@+id/textViewLoc6"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="245dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/tremors"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc5"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel6" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/buttonView3"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginTop="15dp"
            android:orientation="horizontal"
            app:layout_constraintTop_toBottomOf="@+id/buttonView2"
            tools:ignore="NotSibling">

            <Button
                android:id="@+id/buttonSel7"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_fast_heart_beat_selected"
                android:onClick="heartRateClick"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent" />

            <TextView
                android:id="@+id/textViewLoc7"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="45dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/fast_heart_rate"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel7" />

            <Button
                android:id="@+id/buttonSel8"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="160dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_fainting_selected"
                android:onClick="faintingClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel7" />

            <TextView
                android:id="@+id/textViewLoc8"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="145dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/fainting"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc7"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel8" />

            <Button
                android:id="@+id/buttonSel9"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="260dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_irritability_selected"
                android:onClick="irritabilityClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel8" />

            <TextView
                android:id="@+id/textViewLoc9"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="245dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/irritability"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc8"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel9" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/buttonView4"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginTop="15dp"
            android:orientation="horizontal"
            app:layout_constraintTop_toBottomOf="@+id/buttonView2"
            tools:ignore="NotSibling">

            <Button
                android:id="@+id/buttonSel10"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_fast_heart_beat_selected"
                android:onClick="heartRateClick"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent" />

            <TextView
                android:id="@+id/textViewLoc10"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="45dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/fast_heart_rate"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel10" />

            <Button
                android:id="@+id/buttonSel11"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="160dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_fainting_selected"
                android:onClick="faintingClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel10" />

            <TextView
                android:id="@+id/textViewLoc11"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="145dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/fainting"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc10"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel11" />

            <Button
                android:id="@+id/buttonSel12"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="260dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ic_irritability_selected"
                android:onClick="irritabilityClick"
                app:layout_constraintLeft_toLeftOf="@id/buttonSel11" />

            <TextView
                android:id="@+id/textViewLoc12"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="245dp"
                android:layout_marginTop="60dp"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/irritability"
                android:textAlignment="center"
                android:textColor="@color/dialogHeaderLight"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="@+id/textViewLoc11"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonSel12" />

        </RelativeLayout>

    </LinearLayout>

</ScrollView>

<Button
    android:id="@+id/EnterButton"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:background="@drawable/button_purple_rounded"
    android:drawablePadding="25dp"
    android:layout_marginTop="20dp"
    android:fontFamily="sans-serif-condensed"
    android:gravity="center_horizontal|center_vertical"
    android:onClick="enterDose"
    android:paddingEnd="24dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingStart="24dp"
    android:text="@string/enter"
    android:textAlignment="gravity"
    android:textColor="@color/dialogWhite"
    android:textStyle="bold"
    app:layout_constraintTop_toBottomOf="@+id/scrollViewWB"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

问题是只有第一行按钮出现(“@+id/buttonView”)其余三行没有出现。我试过调整 Scrollview 的大小,但我无法让所有四行都出现在 Scrollview 中。我做错了什么?

只需添加:

android:orientation="vertical"

给你的

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