带有方形子项的方形 GridLayout(边=父宽度)

Square GridLayout with square children (side=parent width)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:orientation="horizontal">


    <GridLayout
        android:id="@+id/foursquares"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:columnCount="8"
        android:orientation="vertical"
        android:rowCount="8"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/a1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_row="0"
            android:layout_rowSpan="1"
            android:layout_column="0"
            android:layout_columnSpan="1"
            android:background="@drawable/blacksquare"
            android:orientation="horizontal"></LinearLayout>

        <LinearLayout
            android:id="@+id/a2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_row="0"
            android:layout_rowSpan="1"
            android:layout_column="1"
            android:layout_columnSpan="1"
            android:background="@drawable/whitesquare"
            android:orientation="horizontal"></LinearLayout>

        <LinearLayout
            android:id="@+id/a3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_row="1"
            android:layout_rowSpan="1"
            android:layout_column="0"
            android:layout_columnSpan="1"
            android:background="@drawable/whitesquare"
            android:orientation="horizontal"></LinearLayout>

        <LinearLayout
            android:id="@+id/a4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_row="1"
            android:layout_rowSpan="1"
            android:layout_column="1"
            android:layout_columnSpan="1"
            android:background="@drawable/blacksquare"
            android:orientation="horizontal"></LinearLayout>


    </GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

ConstraintLayout is allows you to make large and complex layouts with a flat view hierarchy. No nested view groups like inside RelativeLayout or LinearLayout etc. You can make Responsive UI for android using ConstraintLayout and its more flexible compare to RelativeLayout and Grid Layout的主要优势。

ConstraintLayout 使您可以轻松地将任何视图约束到 percentage width or height。所以我稍微编辑了你的代码。看看兄弟,希望对你有帮助

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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:orientation="horizontal"
        android:padding="16dp">

            <LinearLayout
                android:id="@+id/a1"
                android:layout_width="0dp"
                app:layout_constraintWidth_percent=".48"
                android:layout_height="0dp"
                app:layout_constraintHeight_percent=".3"
                android:background="@drawable/blacksquare"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:padding="8dp"
                android:orientation="horizontal">
            </LinearLayout>

            <LinearLayout
                android:id="@+id/a2"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintWidth_percent=".48"
                app:layout_constraintHeight_percent=".3"
                android:background="@drawable/blacksquare"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:padding="8dp"
                android:orientation="horizontal">
            </LinearLayout>

            <LinearLayout
                android:id="@+id/a3"
                android:layout_width="0dp"
                app:layout_constraintWidth_percent=".48"
                android:layout_height="0dp"
                app:layout_constraintHeight_percent=".3"
                android:background="@drawable/blacksquare"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/a1"
                android:layout_marginTop="16dp"
                android:orientation="horizontal">
            </LinearLayout>

            <LinearLayout
                android:id="@+id/a4"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintWidth_percent=".48"
                app:layout_constraintHeight_percent=".3"
                android:background="@drawable/blacksquare"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@id/a2"
                android:orientation="horizontal">
            </LinearLayout>

        <LinearLayout
            android:id="@+id/a5"
            android:layout_width="0dp"
            app:layout_constraintWidth_percent=".48"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent=".3"
            android:background="@drawable/blacksquare"
            android:layout_marginTop="16dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/a3"
            android:orientation="horizontal">
        </LinearLayout>

        <LinearLayout
            android:id="@+id/a6"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintWidth_percent=".48"
            app:layout_constraintHeight_percent=".3"
            android:background="@drawable/blacksquare"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@id/a4"
            android:orientation="horizontal">
        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>