Not its separate components, but the dimension ratio of the
ConstraintLayout
itself, or a group of elements together.
I'm fallowing this excellent tutorial to build a RecyclerView
with GridLayoutManager
and two CardView
columns that span the entire width of their container, each CardView
(square) contains an ImageView
(also square) and a TextView
.
So, I need the CardView
width to be "match parent" and get the same height (to be square), and the same goes for the ImageView
. Something like this:
But this is what I have so far (note that CardView
are not square):
This is my item layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_home_black_24dp" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
I know about app:layout_constraintDimensionRatio="1:1"
, but this is for use on any element within a ConstraintLayout
, for example on my ImageView
, not on ConstraintLayout
itself.
I think I can invert my layout, put the CardView
inside the ConstraintLayout
and apply the above property to the CardView
, but then I'm going to need another ConstraintLayout
inside the CardView
to place the ImageView
and TextView
properly, and I think it's not a good idea to nest ConstraintLayout
.
Any suggestions?