Search code examples
androidandroid-layoutandroid-tablelayout

Button fill the whole row in TableLayout?


I have a LinearLayout in vertical orientation, in the first row I added one button which fills the parent.

I want to implement the same thing using TableLayout.
What I've tried so far is:

enter image description here

<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout">
<TableRow 
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:id="@+id/firstRow">

<Button
     android:id="@+id/button1"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Button" />
</TableRow >
</TableLayout>

The TableRow fills the parent, but the button inside the row won't fill the whole row.

How to make the button fill the parent like this?


Edit:

And how to make it vertically fill the parent like this:

enter image description here


Solution

  • use android:layout_weight="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" >
    
        <TableRow>
    
            <Button
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Why is doing layouts on Android" />
        </TableRow>
    
        <TableRow>
    
            <Button
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="damn" />
        </TableRow>
    
    </TableLayout>
    

    looks like :

    enter image description here