为什么按钮之间的距离在 Android Studio 的设计视图和设备上不匹配?

Any reason why the distance between buttons do not match in design view and on device at Android Studio?

这是我在布局文件中使用的代码。我使用 Material 组件作为主题样式。这里我同时使用了 material 组件按钮和按钮,但我认为由于我的主题风格,它们都被转换为 material 按钮:

<?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/createAccountBtn"
    android:layout_width="238dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="86dp"
    android:layout_marginTop="35dp"
    android:layout_marginEnd="87dp"
    android:layout_marginBottom="318dp"
    android:text="@string/create_account"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/signInBtn" />

<Button
    android:id="@+id/signInBtn"
    android:layout_width="237dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="89dp"
    android:layout_marginTop="282dp"
    android:layout_marginEnd="85dp"
    android:layout_marginBottom="14dp"
    android:text="@string/common_signin_button_text"
    app:layout_constraintBottom_toTopOf="@+id/createAccountBtn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

仿真器型号和您设计应用程序所针对的型号可能不同! 请检查并回复。

显然我需要为 layout_widthlayout_height 使用 match_parent 和 wrap_content 属性。可以在此处查看与 ConstraintLayout 一起使用的此文档:https://developer.android.com/training/multiscreen/screensizes

但我建议使用 LinearLayout 作为 ConstraintLayout 在我的情况下表现仍然很差,因为我必须以精确的 dps 定义边距属性,这可能与特定屏幕尺寸不一致。我在下面添加了 LinearLayout 解决方案,它在纵向和横向模式下也能按预期工作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/createAccountBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/create_account" />

    <Button
        android:id="@+id/signInBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/common_signin_button_text"/>
</LinearLayout>