为什么我不能将按钮位置设置在同一行并相互绑定?

Why can't i set button position at the same line and tied to each other?

我正在尝试将两个按钮对齐,使其在同一位置 "line"(在 x 轴上没有任何间隙)。出于某种原因,我不明白为什么按钮不在同一行。 我想了解我做错了什么

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:text="Button"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
 app:layout_constraintTop_toBottomOf="@+id/button"

应该是

 app:layout_constraintBottom_toBottomOf="@+id/button"

在 button2 中约束应该是

    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintStart_toEndOf="@+id/button"

像这样

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    tools:context=".MainActivity">

<Button
        android:id="@+id/button"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintStart_toEndOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

根据需要设置边距button2。现在它在同一行。

替换

app:layout_constraintTop_toBottomOf="@+id/button" 

(因为这会将按钮 2 的顶部设置为按钮(1)的底部。

来自 button2 和这些

app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

因为您使用此方法将底部按钮与第一个按钮的末端对齐 app:layout_constraintStart_toEndOf="@+id/button" 但你需要像下面那样首先开始和结束你的屁股

app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintStart_toStartOf="@+id/button"

完整代码

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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="match_parent"
        tools:context=".MainActivity">

        <Button
                    android:id="@+id/button"
                    android:layout_width="180dp"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button"
                    app:layout_constraintTop_toBottomOf="@+id/button"
                    app:layout_constraintEnd_toEndOf="@+id/button"
                    app:layout_constraintStart_toStartOf="@+id/button" />
    </androidx.constraintlayout.widget.ConstraintLayout>