【问题标题】:Square GridLayout with square children (side=parent width)Square GridLayout 带有方形子项(边=父宽度)
【发布时间】:2020-01-18 00:15:17
【问题描述】:
  • 我正在尝试制作一个方形网格布局(嵌套在约束布局中),所有边都等于父宽度,这样对于任何设备,用户都将拥有最大可能的正方形屏幕。

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

  • 理想情况下,我想在 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>

【问题讨论】:

    标签: java android android-gridlayout


    【解决方案1】:

    ConstraintLayout 的主要优点是允许您使用平面视图层次结构制作大而复杂的布局。没有嵌套视图组,如 RelativeLayout 或 LinearLayout 等。您可以使用 ConstraintLayout 为 android 制作响应式 UI,与 RelativeLayoutGrid Layout 相比,它更灵活。 p>

    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>
    

    【讨论】:

    • 谢谢你的朋友,你的代码运行良好!我的问题相当简单。该项目是一个国际象棋应用程序,我已经编写了很大一部分代码来使用网格布局。我正在寻找一个快速修复,但是如果这是一个死胡同,我可能会考虑使用不同的布局。是否有理由为此目的更喜欢 Constraint-Layout 而不是 Grid-Layout?
    • 如果我没记错的话,它会使视图层次结构变平并提供更好的性能。
    猜你喜欢
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多