I have a gridlayout
with 3 rows
and 3 columns
(we make a tic tac toe game).
Inside these rows
and columns
should be ImageViews
. However: My teacher centers them by changing the margin
of every ImageView
manually.
I think thats not the best way to do that, especially when having a larger amount of rows
and columns
as we do in this example.
Is there a better way to center
all ImageViews
inside the rows
/columns
?
The xml
code is (with the first 3 images):
<androidx.gridlayout.widget.GridLayout
android:layout_width="368dp"
android:layout_height="368dp"
android:background="@drawable/board"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
app:layout_column="0"
app:layout_row="0"
app:srcCompat="@drawable/coin_red" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="14dp" // This one we edit manually ...
app:layout_column="1"
app:layout_row="0"
app:srcCompat="@drawable/coin_red" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="12dp"
app:layout_column="2"
app:layout_row="0"
app:srcCompat="@drawable/coin_red" />
</androidx.gridlayout.widget.GridLayout>