views:

12

answers:

1

I'm wrestling with the Android UI to try to create a table layout that has two rows with two buttons in each row. I want the buttons to each take up 50% of the space in the row. However, no matter what I set the layout_width and layout_height to, I always get two skinny buttons in each row.

<?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"
    >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
   android:orientation="horizontal"
  android:id="@+id/tablelayout" >
<TableRow>
<Button  
    android:id="@+id/btn_one" 
    android:tag="1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text=""/>
<Button  
    android:id="@+id/btn_two" 
    android:tag="2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="" 
    android:padding="0px"/>
</TableRow>
<TableRow>
<Button  
    android:id="@+id/btn_three" 
    android:tag="3" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text=""/>
<Button  
    android:id="@+id/btn_four" 
    android:tag="4" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text=""/>
</TableRow>
</TableLayout>
</LinearLayout>
A: 

The answer is to use the magic layout_weight parameter.

<?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"
    >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
   android:orientation="horizontal"
  android:id="@+id/tablelayout" >
<TableRow
    android:layout_weight="1">
<Button  
    android:id="@+id/btn_one" 
    android:tag="1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:text=""/>
<Button  
    android:id="@+id/btn_two" 
    android:tag="2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:text="" 
    android:padding="0px"/>
</TableRow>
<TableRow
    android:layout_weight="1"
>
<Button  
    android:id="@+id/btn_three" 
    android:tag="3" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:text=""/>
<Button  
    android:id="@+id/btn_four" 
    android:tag="4" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:text=""/>
</TableRow>
</TableLayout>
</LinearLayout>
gregm