I need to get a TableLayout to match the width of it's parent LinearLayout, but I cannot get it to work.
In the first screenshot below, the "Short header" is the first view in a vertical LinearLayout. The "Fields" below are members of a TableLayout which is the second view in the vertical LinearLayout.
When the "Header" text does not require as much width as the TableLayout, everything aligns as intended.
When the "Header" requires more width than the TableLayout, the TableLayout does not match_width with the the "Header", as you can see in the second screenshot.
How can I make the TableLayout match the width of the LinearLayout?
Note: The base LinearLayout needs to "wrap" to the smallest width required to display it's contents, so setting a fixed width is not a solution
The layout XML for this test:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/baseLinerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFAAAAAA" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:background="#FF000000"
android:textColor="#FFFFFFFF"
android:text="Long header to make base layout wider than table layout" />
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#33FFFFFF" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:gravity="center"
android:padding="10dp"
android:background="#44FFFFFF"
android:textColor="#FF000000"
android:text="Field (0,0)" />
<TextView
android:gravity="center"
android:padding="10dp"
android:background="#88FFFFFF"
android:textColor="#FF000000"
android:text="Field (0,1)" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:gravity="center"
android:padding="10dp"
android:background="#88FFFFFF"
android:textColor="#FF000000"
android:text="Field (0,1)" />
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>