在Android中创建网格状布局

在Android中创建网格状布局,android,xml,android-layout,grid-layout,android-layout-weight,Android,Xml,Android Layout,Grid Layout,Android Layout Weight,基本上,我有一个视图,需要分成2x4网格,每个部分大小相等(即每行为屏幕高度的25%,每列为屏幕宽度的50%) 我的方法是使用一个带有两个权重为0.5的内部线性布局的水平线性布局,然后在每个线性布局中,将孩子的权重设置为0.25,以便每个孩子占据屏幕的25% 虽然这似乎是可行的,但这显然对性能非常不利(请参阅此线程了解原因) 有没有其他办法来实现这一点?我环顾了一下,但找不到一个纯XML的解决方案 下面是如何设置LinearLayouts及其子项的代码示例 <?xml version="1

基本上,我有一个视图,需要分成2x4网格,每个部分大小相等(即每行为屏幕高度的25%,每列为屏幕宽度的50%)

我的方法是使用一个带有两个权重为0.5的内部线性布局的水平线性布局,然后在每个线性布局中,将孩子的权重设置为0.25,以便每个孩子占据屏幕的25%

虽然这似乎是可行的,但这显然对性能非常不利(请参阅此线程了解原因)

有没有其他办法来实现这一点?我环顾了一下,但找不到一个纯XML的解决方案

下面是如何设置LinearLayouts及其子项的代码示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:weightSum="1.0"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.5"
        android:weightSum="1.0">        
            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />

            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />

            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />

            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.5"
        android:weightSum="1.0">        
            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />

            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />

            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />

            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:src="@drawable/example"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.25"
            />
    </LinearLayout>
</LinearLayout>

您可能想让这台机器旋转一下


此外,还有一个功能可用于1.6+设备。

在LinearLayout内部使用GridView。选中

很遗憾,我需要能够设置行数,以便自动调整高度,因此在这种特定情况下我无法使用GridView:(