如何在父级为滚动视图的约束布局中将元素一个接一个地放置

How to place elements one below another in constraint layout whose parent is scroll view

我吃的是什么:

我有 4 个小部件,放置在约束布局中,其父级是滚动视图


我想做什么:

我正在尝试一个一个地显示小部件


发生了什么:

小部件一个放在另一个上面


MyLayout

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <EditText
            android:id="@+id/edtNameId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_name"
            android:inputType="text"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <EditText
            android:id="@+id/edtPwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_password"
            app:layout_constraintBottom_toBottomOf="@+id/edtNameId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <TextView
            android:id="@+id/txtChangePwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/str_change_password"
            app:layout_constraintBottom_toBottomOf="@+id/edtPwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <Button
            android:id="@+id/btnSubmitId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/str_submit"
            app:layout_constraintBottom_toBottomOf="@+id/txtChangePwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

输出:

您应该使用 layout_constraintTop_toBottomOf 而不是 app:layout_constraintBottom_toBottomOf 试试这个

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <EditText
            android:id="@+id/edtNameId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_name"
            android:inputType="text"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <EditText
            android:id="@+id/edtPwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_password"
            app:layout_constraintTop_toBottomOf="@+id/edtNameId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <TextView
            android:id="@+id/txtChangePwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/str_change_password"
            app:layout_constraintTop_toBottomOf="@+id/edtPwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <Button
            android:id="@+id/btnSubmitId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/str_submit"
            app:layout_constraintTop_toBottomOf="@+id/txtChangePwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>