表布局中的等高行

时间:2014-03-05 20:41:15

标签: 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_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>

2 个答案:

答案 0 :(得分:1)

试试这个:

<?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>

答案 1 :(得分:0)

我对上一个TableRow进行了以下更改,一切顺利

<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 height)。

相关问题