【发布时间】:2021-02-18 17:38:59
【问题描述】:
我有一个 gridlayout 和 3 个 rows 和 3 个 columns(我们制作井字游戏)。
在这些rows 和columns 中应该是ImageViews。但是:我的老师通过手动更改每个ImageView 的margin 来使它们居中。
我认为这不是最好的方法,尤其是当我们在本例中使用大量 rows 和 columns 时。
有没有更好的方法来centerImageViews 内的rows/columns?
xml 代码是(前 3 张图片):
<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>
【问题讨论】:
-
这是你的答案吗? (stackoverflow.com/questions/4592065/…)
-
不是真的,我的图片都有相同的大小,所以这不是问题。我只想使用一个网格,其中每个图像都以自己的网格单元为中心。这在带有 xml 的 Visual Studio 中很容易做到,但我还没有找到关于如何在 android Studio 中做到这一点的解决方案。
标签: java xml android-studio