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

标签 java android android-gridlayout

  • 我正在尝试制作一个方形网格布局(嵌套在约束布局中),所有边都等于父宽度,这样对于任何设备,用户都将在他的设备上拥有最大可能的正方形屏幕。

  • 然后我想在第一个正方形中再添加四个正方形,它们将是带有背景图像的线性布局,它将根据网格布局单元格大小进行拉伸(stretch)。

  • 理想情况下,我想在 XML 中执行此操作。我是 android 的初学者,请原谅我在以下代码中可能存在的明显问题和错误,但我在这方面花费了大量时间,没有任何进展。如有任何帮助,我们将不胜感激。

  • 下面是我的 main.xml,其中单元格具有固定尺寸。我尝试过改变重量和重力,但没有成功。只是想知道是否有一种简单的方法可以使这段代码执行我想要的操作。

<?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 允许您使用平面 View 层次结构制作大型且复杂的布局。没有像RelativeLayout或LinearLayout等那样的嵌套 View 组。您可以使用ConstraintLayout为android制作响应式UI,并且与 RelativeLayout相比它更灵活Grid Layout .

ConstraintLayout 允许您轻松地将任何 View 限制为百分比宽度或高度。所以我稍微编辑了你的代码。兄弟看看吧,希望对你有帮助。

<?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>

关于java - 带有方形子项的方形 GridLayout(边=父宽度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57969041/

相关文章:

java - 绑定(bind)Spring MVC命令对象时如何自定义参数名称?

android - glReadPixels 很慢

android - 在 ListView 的 ArrayAdapter 中使用 Linkify 导致 RuntimeException

java - 如何在同一个对话框中显示直接输入和排序的ArrayList

Java 内联 int 交换。为什么这只适用于 Java

android - 创建一个将根据操作动态刷新的 AlertDialog

android - Android 的六角板 - 如何做?

android - 在 gridLayout 中从右到左放置项目

android - 在网格布局中调整 imageViews 的大小

java - AspectJ - 避免切入点间接调用自己