Android Studio 网格布局无法正常工作
Android Studio Grid Layout Not Working Properly
我正在尝试制作 2 列 3 行的图片网格。但是我的屏幕只显示一个 item.I 我不知道为什么。
这是我得到的:
这就是我要实现的目标:
到目前为止,这是我的代码。我正在关注 YouTube 上的一个教程,并且我已经重新检查了多次。我仍然不确定出了什么问题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context=".Interests">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_weight="2">
<TextView
android:id="@+id/textGrid"
style="@style/HeaderStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text="Select your interests"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:columnCount="2"
android:rowCount="3"
android:alignmentMode="alignMargins"
android:padding="14dp"
>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_walking_with_dog">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_row="0"
android:layout_column="0"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:src="@drawable/ic_walking_with_dog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_row="0"
android:layout_column="0"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:src="@drawable/ic_walking_with_dog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
非常感谢您的帮助。谢谢!
您没有正确设置 GridLayout children 的 ROW 和 COLUMN。尝试如下设置:
第一个 Child 与 0,0:
<androidx.cardview.widget.CardView
android:layout_row="0"
android:layout_column="0"
...>
第二个 Child 与 0,1:
<androidx.cardview.widget.CardView
android:layout_row="0"
android:layout_column="1"
...>
第三个 Child 与 1,0:
<androidx.cardview.widget.CardView
android:layout_row="1"
android:layout_column="0"
...>
第四个 Child 与 1,1:
<androidx.cardview.widget.CardView
android:layout_row="1"
android:layout_column="1"
...>
我正在尝试制作 2 列 3 行的图片网格。但是我的屏幕只显示一个 item.I 我不知道为什么。
这是我得到的:
这就是我要实现的目标:
到目前为止,这是我的代码。我正在关注 YouTube 上的一个教程,并且我已经重新检查了多次。我仍然不确定出了什么问题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context=".Interests">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_weight="2">
<TextView
android:id="@+id/textGrid"
style="@style/HeaderStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text="Select your interests"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:columnCount="2"
android:rowCount="3"
android:alignmentMode="alignMargins"
android:padding="14dp"
>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_walking_with_dog">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_row="0"
android:layout_column="0"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:src="@drawable/ic_walking_with_dog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_row="0"
android:layout_column="0"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:src="@drawable/ic_walking_with_dog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
非常感谢您的帮助。谢谢!
您没有正确设置 GridLayout children 的 ROW 和 COLUMN。尝试如下设置:
第一个 Child 与 0,0:
<androidx.cardview.widget.CardView
android:layout_row="0"
android:layout_column="0"
...>
第二个 Child 与 0,1:
<androidx.cardview.widget.CardView
android:layout_row="0"
android:layout_column="1"
...>
第三个 Child 与 1,0:
<androidx.cardview.widget.CardView
android:layout_row="1"
android:layout_column="0"
...>
第四个 Child 与 1,1:
<androidx.cardview.widget.CardView
android:layout_row="1"
android:layout_column="1"
...>