Reputation: 11
I want to align the lower views to the left edge of the top view
enter the image description here
but they go away like this:
<?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">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginStart="56dp"
android:layout_marginTop="24dp"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_code" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_code"
tools:layout_editor_absoluteX="56dp"
tools:layout_editor_absoluteY="174dp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_code"
tools:layout_editor_absoluteX="56dp"
tools:layout_editor_absoluteY="333dp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_code"
tools:layout_editor_absoluteX="56dp"
tools:layout_editor_absoluteY="501dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have done it many times but it keeps doing this.
Upvotes: 0
Views: 33
Reputation: 112
The constraint layout you posted is not accurate and correct. All the views are not constrained to each other.
Here is how we do it:
<?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"
android:paddingStart="56dp"> // this aligns all the child views to 56 dp from the Start
<ImageButton
android:id="@+id/imageButton"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="24dp"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_code" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="30dp" // add the required dp for gap between 2nd and 1st image
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_code"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="30dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_code"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton1" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="30dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_code"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Please constrain your child views as per your requirement, constraint layout gives a vast variety of constraining options, I suggest you to refer to the documentation of Android: https://developer.android.com/develop/ui/views/layout/constraint-layout
Upvotes: 1