GridLayout合并左上角的所有元素

时间:2018-11-01 11:16:29

标签: android android-gridlayout

我正在尝试创建一个简单的井字游戏,为此我正在使用GridLayout来制作棋盘格。当我将imageView放入GridLayout时,它们全部合并到gridLayout的左上角。我在API-21上使用gridLayout。我尝试过可以在互联网上找到的所有可能的解决方案。 这是我的活动的XML。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
tools:context=".inGameActivity">

<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@drawable/background"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3">


    <ImageView
        android:id="@+id/imageView1"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView2"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView3"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView4"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView5"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView6"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView7"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView8"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

    <ImageView
        android:id="@+id/imageView9"
        app:layout_gravity="fill"
        app:srcCompat="@drawable/x" />

</android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

您需要更改layout_gravity,以使元素保留在GridLayout上所需的位置。通过将fill更改为网格的位置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_dark"
    tools:context=".MainActivity">

    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:background="@drawable/background"
        app:columnCount="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rowCount="3">

        <ImageView
            android:id="@+id/imageView1"
            app:layout_gravity="top|left"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView2"
            app:layout_gravity="center_horizontal"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView3"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView4"
            app:layout_gravity="left|center_horizontal"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView5"
            app:layout_gravity="center_horizontal"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView6"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView7"
            app:layout_gravity="left"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView8"
            app:layout_gravity="center_horizontal"
            app:srcCompat="@drawable/x" />

        <ImageView
            android:id="@+id/imageView9"
            app:srcCompat="@drawable/x" />

    </android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>