android Gridlayout如何将所有图像对齐为3 X 2网格?

问题描述

如何创建将图像对齐为3行2列的网格布局。 我尝试使用linearlayout进行布局,但由于必须指定高度和宽度,因此无法正常工作。


 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

 <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

 <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/mycolor"
            tools:src="@drawable/image" />

 <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/mycolor"
            tools:src="@drawable/image" />

</LinearLayout>


 <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

 <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/mycolor"
            tools:src="@drawable/image" />

 <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/mycolor"
            tools:src="@drawable/image" />

</LinearLayout>

 <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

 <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/mycolor"
            tools:src="@drawable/image" />

 <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@color/mycolor"
            tools:src="@drawable/image" />

</LinearLayout>

</LinearLayout>

这可行,但不适用于所有屏幕尺寸,有人可以建议如何为图像构建3 X 2的网格布局吗?

解决方法

为方便实施,您可以使用RecyclerView在固定列中将数据显示为网格。

我建议您阅读CodeLabs以获得快速模式。