I need to place 4 images into GridLayout
. If I define their sizes hard-coded.
e.g.
android:layout_width="150dp"
android:layout_height="200dp"
It works fine. But my question is, if is it possible to do it without defining sizes? Meanwhile I've tried this:
<GridLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="16dp"
android:columnCount="2"
android:orientation="horizontal"
android:useDefaultMargins="true"
app:layout_constraintTop_toBottomOf="@+id/top_object"
app:layout_constraintBottom_toTopOf="@+id/bottom_object"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/poster_21"
android:scaleType="centerCrop"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/poster_22"
android:scaleType="centerCrop"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/poster_23"
android:scaleType="centerCrop"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/poster_24"
android:scaleType="centerCrop"/>
</GridLayout>
But in this case I have left top image occupies the whole Grid Layout and 3 other images have the same size and are located outside the screen.
You can use 0dp
width & height for each GridLayout
element, i.e. ImageView
And control the width & height using the column & row weights
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/tools"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="16dp"
android:useDefaultMargins="true"
android:layout_constraintTop_toBottomOf="@+id/top_object"
android:layout_constraintBottom_toTopOf="@+id/bottom_object"
android:layout_constraintLeft_toLeftOf="parent"
android:layout_constraintRight_toRightOf="parent"
android:columnCount="2">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/poster_21"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
grid:layout_gravity="center" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/poster_22"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
grid:layout_gravity="center" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/poster_23"
android:text="item 0x2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
grid:layout_gravity="center" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/poster_24"
android:text="item 1x0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
grid:layout_gravity="center" />
</GridLayout>