Android 表布局中的等高行

Android 表布局中的等高行,android,android-layout,android-tablelayout,android-layout-weight,Android,Android Layout,Android Tablelayout,Android Layout Weight,我想制作一个可重用的表布局,其中我有3行,最后一行只包含1个元素,而前两行各包含2个元素 现在,问题是,表布局中的行并不是高度相等的,尽管每行的权重相等 最下面一行占用了太多的空间 布局内容如下: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match

我想制作一个可重用的表布局,其中我有3行,最后一行只包含1个元素,而前两行各包含2个元素

现在,问题是,表布局中的行并不是高度相等的,尽管每行的权重相等

最下面一行占用了太多的空间

布局内容如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/age" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_roll_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/roll_no" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_standard"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/standard" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_section"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/section" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

        <TextView
            android:id="@+id/txtvw_ct_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="@string/ct_name" >
        </TextView>
    </TableRow>
</TableLayout>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_roll_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_standard"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_section"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TextView
            android:id="@+id/txtvw_ct_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>
    </TableRow>
</TableLayout>

我对最后一张
表格行做了以下更改,一切都很顺利

<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"  
    android:weightSum="1"   
    >

    <TextView
        android:id="@+id/txtvw_ct_name"
        android:layout_width="0dp"      // this t.v will consume all width
        android:layout_weight="1"
        android:layout_height="match_parent"  // consume all height as of parent which is adjusted by weight.
        android:text="@string/save" 
        >
    </TextView>
</TableRow>


由于表格布局的权重和为3,我为表格行提供了相等的权重(w.r.t高度)。

忽略
表格行的任何高度和宽度设置,而
fill\u parent
TableLayout
自动设置为
layout\u width
wrap\u content
layout\u height
您的更改似乎运行良好,但我无法理解这些更改是如何运行的。如果你也添加一些描述就好了。不过,我还有一个解决方案,我也会把它作为答案。