Monday, February 21, 2011

Buttons to fill width when using TableLayout

Hello,

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <TableLayout android:id="@+id/TableLayout01"
        android:layout_height="wrap_content" android:layout_width="fill_parent">
        <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="Settings"
                android:id="@+id/btnSettings"></Button>
            <Button android:text="@+id/Button01" android:id="@+id/Button01"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            <Button android:text="@+id/Button02" android:id="@+id/Button02"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        </TableRow>
        <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button android:text="@+id/Button03" android:id="@+id/Button03"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            <Button android:text="@+id/Button04" android:id="@+id/Button04"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
            <Button android:text="@+id/Button05" android:id="@+id/Button05"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        </TableRow>
    </TableLayout>
</ScrollView>

I have a table having 2 rows each row having 3 buttons. How can I make the buttons to fill the space equally. In HTML I would give them 33% width.

Also do you know any way I can create a view having 4 image buttons in a row as a grid layout, similar to the launcher.

From stackoverflow
  • Try adding android:stretchColumns="*" to your <TableLayout> tag.

  • Set the TableRow layout_width to fill_parent and set a layout_weight of 1 on each button.

    The layout_weight works sort of like a percentage. If all of your items get the same number, they take the same percent of space. If one button has a weight of 2, and another has a weight of 1, then the first will take up twice as much space.

    If you haven't done so already, ready through the common layouts page of the dev guide for a good intro to layouts.

0 comments:

Post a Comment