Android 在不同行中具有不同列的TableLayout

Android 在不同行中具有不同列的TableLayout,android,Android,我尝试创建一个包含多行的TableLayout,每行都有不同的列 下面显示列表视图。对于列表视图的每一行,它都包含一个只有1行2列的表布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_h

我尝试创建一个包含多行的TableLayout,每行都有不同的列

下面显示列表视图。对于列表视图的每一行,它都包含一个只有1行2列的表布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/color_label"
        android:layout_width="12dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:background="#ffffff" />

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="1">
        <TableRow>
            <TextView
                android:id="@+id/toptext"
                android:gravity="left"
            />
            <TextView
                android:id="@+id/bottomtext"
                android:gravity="right"
            />       
        </TableRow>        

        <View android:layout_height="2dip"
            android:background="#FF909090" />
    </TableLayout>   
</LinearLayout>

以下是结果

稍后,我想进一步改进TableLayout。它的第一行有两列。它的第二行有4列,一条垂直线将1,2列与3,4列分开

我使用以下代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/color_label"
        android:layout_width="12dip"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:background="#ffffff" />

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="1">
        <TableRow>
            <TextView
                android:id="@+id/toptext"
                android:gravity="left"
            />
            <TextView
                android:id="@+id/bottomtext"
                android:gravity="right"
            />       
        </TableRow>        

        <View android:layout_height="2dip"
            android:background="#FF909090" />

        <TableRow>
            <TextView
                android:id="@+id/col1"
                android:gravity="left"
                android:text="Column 1"
            />
            <TextView
                android:id="@+id/col2"
                android:gravity="right"
                android:text="Column 2"
            />
            <TextView
                android:id="@+id/col3"
                android:gravity="left"
                android:text="Column 3"
            />
            <TextView
                android:id="@+id/col4"
                android:gravity="right"
                android:text="Column 4"
            />            
        </TableRow>        


        <View android:layout_height="2dip"
            android:background="#FF909090" />

    </TableLayout>   
</LinearLayout>

以下是结果

但是,表布局的第二行不是我想要的。我想要的是这样的东西


如何改进XML以达到上述效果?

您可以为不同的项目渲染器创建自己的可绘制XML(在
res/drawable-[h | m | l]dpi
中),并将其设置为背景


通过这种方式,您可以实现任何类型的边框、形状等,在您的
适配器
getView
方法中,您只需使用适当的
backgroundDrawable

来膨胀所需的视图,我也有相同的要求,但是如果我有单独的XML,并且我将表格水平滚动,那么它就无法正常移动。我所做的是在一个XML中声明标题,在一个XML中声明行,我在活动中膨胀XML,但现在发生的是两个VIE,所以当我移动底部视图(即列表视图)时发生的情况上面的视图表示列没有移动。我们不能将listview放在滚动视图中。