Search code examples
androidbuttonandroid-tablelayoutspacing

How could I divide a table row into three exact parts?


Here is the XML code for the button part for my table row in a table layout:

<TableRow
    android:id="@+id/tableRow8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
        android:id="@+id/buttonclear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear" 
        android:layout_weight="1" android:textSize="18sp"/>
    <Button
        android:id="@+id/buttonback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back" 
        android:layout_weight="1" android:textSize="18sp"/>
    <Button
        android:id="@+id/buttonconfirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Confirm" 
        android:layout_weight="1" android:textSize="18sp"/>

</TableRow>

Without inputting constants dip, how could I divide those button evenly into three? I got no luck in experimenting with the weight though.


Solution

  • You may want to use the android:stretchColumns attribute on the TableLayout. As it is zero-based attribute, it should get the value "0,1,2" in your case.

    So your XML should probably look as follows:

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1,2">
        <TableRow
            android:id="@+id/tableRow8">
            <Button
                android:id="@+id/buttonclear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Clear" 
                android:textSize="18sp"/>
            <Button
                android:id="@+id/buttonback"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back" 
                android:textSize="18sp"/>
            <Button
                android:id="@+id/buttonconfirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Confirm" 
                android:textSize="18sp"/>
        </TableRow>
    </TableLayout>
    

    Hope it helps.