如何在父级为滚动视图的约束布局中将元素逐个放置在另一个元素下面

Dev*_*ath 2 android android-constraintlayout

我所拥有的:

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


我正在尝试做的事情:

我正在尝试将小部件一个一个地显示在另一个下面


怎么了:

小部件一个一个地放置在另一个之上


我的布局

<?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>
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

Côn*_*Hải 5

你应该使用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>
Run Code Online (Sandbox Code Playgroud)