androidx.constraintlayout:constraintlayout:2.1.0中的ImageView布局BUG

ImageView layout BUG in the androidx.constraintlayout:constraintlayout:2.1.0

我正在使用 Android Studio 2020.3.1 补丁 3 和用于显示两行的 ConstraintLayout UI。

这是预期的 UI 布局:

-------------------------------------------------------------------------
first_row_text_view  |   first_row_edit_text   |   first_row_image_view
-------------------------------------------------------------------------
second_row_text_view |   second_row_image_view |   second_row_switch
-------------------------------------------------------------------------

但是second_row_image_view在使用“test.xml”时不在预期的位置,它漂浮在first_row_edit_text的顶部。

这里是 build.gradle 设置:

dependencies {
...
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
...
}

这是测试布局:test.xml

<?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">
    
    <TextView
        android:id="@+id/first_row_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/first_row_edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBaseline_toBaselineOf="@id/first_row_text_view"
        app:layout_constraintEnd_toStartOf="@id/first_row_image_view"
        app:layout_constraintStart_toEndOf="@id/barrier_left_right"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/first_row_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/first_row_edit_text"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/second_row_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier_first_row" />

    <ImageView
        android:id="@+id/second_row_image_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBaseline_toBaselineOf="@id/second_row_text_view"
        app:layout_constraintEnd_toStartOf="@id/second_row_switch"
        app:layout_constraintStart_toEndOf="@id/barrier_left_right"
        app:layout_constraintTop_toBottomOf="@id/barrier_first_row"/>

    <Switch
        android:id="@+id/second_row_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBaseline_toBaselineOf="@id/second_row_text_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/second_row_image_view"
        app:layout_constraintTop_toBottomOf="@id/barrier_first_row" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier_left_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="first_row_text_view,second_row_text_view"
        tools:layout_editor_absoluteX="410dp"
        tools:layout_editor_absoluteY="1dp" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier_first_row"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="first_row_text_view,first_row_edit_text,first_row_image_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

但是如果我将 ImageView (second_row_image_view) 更改为具有相同名称的 EditText,ConstraintLayout 可以在正确的位置显示 EditText。

<EditText
    android:id="@+id/second_row_image_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintBaseline_toBaselineOf="@id/second_row_text_view"
    app:layout_constraintEnd_toStartOf="@id/second_row_switch"
    app:layout_constraintStart_toEndOf="@id/barrier_left_right"
    app:layout_constraintTop_toBottomOf="@id/barrier_first_row"/>

是BUG还是其他?

谢谢。

测试用:

可以将test.xml复制到res/layout,在“代码”视图中打开test.xml,然后点击“second_row_image_view”,最后,切换到“设计”视图,也可以发现second_row_image_view悬浮在first_row_edit_text.

的顶部

是因为app:layout_constraintBaseline_toBaselineOf。基线是 属性 基于文本的视图。 ImageViews 没有基线而 EditTexts 有