I’m using Android Studio 2020.3.1 patch 3 and the ConstraintLayout for displaying two rows UI.
Here is the expected UI latyout:
------------------------------------------------------------------------- first_row_text_view | first_row_edit_text | first_row_image_view ------------------------------------------------------------------------- second_row_text_view | second_row_image_view | second_row_switch -------------------------------------------------------------------------
But the second_row_image_view is not at the expected position when using the “test.xml”, it is floating on the top of the first_row_edit_text.
Here is the build.gradle settings:
dependencies { ... implementation 'androidx.constraintlayout:constraintlayout:2.1.0' ... }
Here is the test layout: 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>
But If I change the ImageView (second_row_image_view)
to EditText
with the same name, the ConstraintLayout can display the EditText at the right place.
<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"/>
Is it a BUG or something else?
Thanks.
For testing:
You can copy the test.xml to the res/layout, open the test.xml in the “Code” view, and then click on the “second_row_image_view”, finally, switch to the “Design” view, you can also find out that the second_row_image_view is floating on the top of the first_row_edit_text.
Advertisement
Answer
It is because of app:layout_constraintBaseline_toBaselineOf
. Baseline is a property of text based views. ImageViews don’t have a baseline while EditTexts do