【发布时间】: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