【问题标题】:How to center elements inside a gridLayout row?如何在 gridLayout 行中居中元素?
【发布时间】:2021-02-18 17:38:59
【问题描述】:

我有一个 gridlayout 和 3 个 rows 和 3 个 columns(我们制作井字游戏)。

在这些rowscolumns 中应该是ImageViews。但是:我的老师通过手动更改每个ImageViewmargin 来使它们居中。 我认为这不是最好的方法,尤其是当我们在本例中使用大量 rowscolumns 时。

有没有更好的方法来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


【解决方案1】:

我想我得到了你想要的效果。它仍然需要很多行,但这样做的好处是它不使用硬编码值。

您需要将每个图像视图的 column_weight 和 row_weight 设置为 1,并将 layout_gravity 设置为“center”。

例如,您可以将 imageView1 更改为

<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:layout_columnWeight="1"
    app:layout_rowWeight="1"
    app:layout_gravity="center"
    app:srcCompat="@drawable/coin_red" />

希望对你有帮助。

【讨论】:

    【解决方案2】:

    根据我的说法,您可以直接将布局重心设置为中心

    <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" 
        android:layout_gravity="center" />
    

    【讨论】:

    • 单独设置“重力”值还没有解决问题,但是在添加了 Nicolas 答案中的所有内容之后,它就起作用了。无论如何感谢您的帮助!
    • 抱歉浪费您的时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多