I'm working on the UI for my first application. I have a TableLayout
with a grid of buttons. There are three buttons in each row. Each
button should take up 1/3 of the horizontal space.

In the last cell of this table, I want two buttons, each taking up
half of the cell's horizontal space (so 1/6 of the screen width in
size). I thought that the way to do this was to put a LinearLayout
into the TableRow, and the two buttons into that. What happens, is
that the cell grows a little bit in size, even though there is plenty
of space to accommodate the two buttons. So the first two buttons are
not 1/3 of the screen width each.

The following XML is a simple case to show the problem. I have three
tables, the first shows the size of the buttons for a regular row with
only three buttons. The second shows what happens when I put one
button into the LinearLayout. This looks good, and is just what I
expect.

The third table shows what happens when I put the second button into
the LinearLayout. Suddenly, the first two buttons are smaller, even
though I see no reason for the LinearLayout to require extra space. By
setting the two buttons inside the LinearLayout to wrap_content, you
can clearly see that there is still extra space to the right of the
fourth button in this table.

I have played with a number of XML attributes in the LinearLayout to
try and get not to expand, but I have had no success.

If anyone knows what I am doing wrong here I would be grateful for
their wisdom.

Thank you for your time.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android" android:orientation="vertical"
              android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- First table, to show relative button sizes -->
  <TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="0,1,2" >
      <TableRow>
          <Button android:id="@+id/button_1" android:text="1"
android:textSize="10.5pt" />
          <Button android:id="@+id/button_2" android:text="2"
android:textSize="10.5pt"  />
          <Button android:id="@+id/button_3" android:text="3"
android:textSize="10.5pt"  />
      </TableRow>
  </TableLayout>
<!-- Second table, one button in the LinearLayout, button sizes ok -->
  <TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="0,1,2" >
      <TableRow>
          <Button android:id="@+id/button_4" android:text="4"
android:textSize="10.5pt" />
          <Button android:id="@+id/button_5" android:text="5"
android:textSize="10.5pt"  />
          <LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:padding="0px"
                        android:layout_marginTop="0px"
android:layout_marginBottom="0px" android:layout_marginLeft="0px"
                        android:layout_marginRight="0px" >
              <Button android:id="@+id/button_6" android:text="6"
android:textSize="10.5pt"
                      android:layout_width="wrap_content"
android:layout_height="fill_parent" />
          </LinearLayout>
      </TableRow>
  </TableLayout>
<!-- Third table, same as second, but with a second button in the
LinearLayout. Now buttons 7 and 8 are smaller. Since the
     two buttons in the LinearLayout do not consume all of the screen
space, the other buttons do not need to be made
     smaller -->
  <TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="0,1,2" >
      <TableRow>
          <Button android:id="@+id/button_7" android:text="7"
android:textSize="10.5pt" />
          <Button android:id="@+id/button_8" android:text="8"
android:textSize="10.5pt" />
          <LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:padding="0px"
                        android:layout_marginTop="0px"
android:layout_marginBottom="0px" android:layout_marginLeft="0px"
                        android:layout_marginRight="0px" >
              <Button android:id="@+id/button_9" android:text="9"
android:textSize="10.5pt"
                      android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<!-- If this button is commented out, buttons 7 and 8 are the right
size, and button 9 is placed in the correct location. -->
              <Button android:id="@+id/button_a" android:text="A"
android:textSize="10.5pt"
                      android:layout_width="wrap_content"
android:layout_height="fill_parent" />
          </LinearLayout>
      </TableRow>
  </TableLayout>
</LinearLayout>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to