[英]Setting row and column size in a gridlayout
I would like to create a 3x3 grid layout in Android Studios for a Tic Tac Toe Game. 我想在Tic Tac Toe游戏的Android Studio中创建3x3网格布局。 However I would like the ImageViews displaying the tiles to be smaller than the actual row´s and columns, so there is a little bit of space in between and it doesn´t look overly crowded.
但是,我希望ImageViews显示的图块小于实际的行和列,因此它们之间留有一点空间,并且看起来不会过于拥挤。 But I haven´t been able to find any answer on how to set specific row and column sizes independent of the content.
但是我没有找到关于如何设置独立于内容的特定行和列大小的任何答案。
I have thought about editing the pictures, so that the tiles don´t fill the whole thing or adding some sort of invisible element that keep the rows and columns at the size I want them to be, but I feel there should be a more straightforward solution. 我曾考虑过编辑图片,以使图块不会填满整个内容或添加某种不可见的元素,以使行和列的大小保持我想要的大小,但是我觉得应该更简单一些解。
On the GridLayout
you can set the rowCount
and columnCount
to 3
and for each ImageView
, set the rowWeight
and columnWeight
to 1
. 在
GridLayout
,可以将rowCount
和columnCount
设置为3
,对于每个ImageView
,将rowWeight
和columnWeight
设置为1
。
As for the spacing, you can set a layout_margin
for each off the ImageView
so they ware evenly spaced. 至于间距,您可以为每个
ImageView
设置一个layout_margin
,以便它们均匀分布。
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:rowCount="3"
android:orientation="horizontal"
android:background="#CCCCCc">
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
<ImageView
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="4dp"
android:background="#FFFFFF"
android:src="@drawable/image"
/>
</GridLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.