Android Studio 设计器预览与在设备上看到的不同

Android Studio designer preview differs from what is seen on device

我在 Android Studio 中有一个布局,它在 Android Studio 的预览中正确呈现,但在实际手机上,它有所不同。 预览:Android Studio preview
Phone 屏幕:Actually seen
我使用一个包含在 FrameLayout 中的 ,它在 运行 时间用不同的内容填充,在这个例子中有一个按钮。重命名字段默认为 GONE,并在按下 rename 筹码时可见。 done 按钮也在卡片的顶部。

XML是:

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

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:text="name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/name">

        <ViewStub
            android:id="@+id/card_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/frameLayout"
        app:layout_constraintVertical_bias="0.0">

        <com.google.android.material.chip.Chip
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="false"
            android:text="delete"
            app:checkedIconVisible="false"
            app:chipIconVisible="false"
            app:closeIconVisible="false" />

        <com.google.android.material.chip.Chip
            android:id="@+id/rename"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkable="false"
            android:text="rename"
            app:checkedIconVisible="false"
            app:chipIconVisible="false"
            app:closeIconVisible="false" />

    </com.google.android.material.chip.ChipGroup>

    <EditText
        android:id="@+id/newname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:hint="Enter new name..."
        android:inputType="text"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/done"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chipGroup"
        app:layout_constraintVertical_bias="0.0"
        tools:visibility="visible" />

    <Button
        android:id="@+id/done"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:text="done"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/newname"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/newname"
        tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

我认为问题是您的框架布局有 android:layout_height="match_parent"。这很可能会导致问题。尝试将高度设置为固定值或包装内容,具体取决于您添加到存根中的视图。

如果我要实现它,我可能会使用垂直 LinearLayout 而不是 ConstraintLayout,并为文本字段和按钮使用第二个 LinearLayout。我发现过度使用 ConstraintLayout 通常会导致奇怪的布局问题。