I'm having a hard time getting wrap_content
to work with TableLayout
. Here is the XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:stretchColumns="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:text="Column 1" />
<Button
android:id="@+id/button1"
android:text="Column 2" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
And this is what it looks like:
As you can see, although layout_weight
of the TableLayout
and the Button
are both 1, the button is crushed to the right.
As discussed here, I know I can solve this by setting the layout_width
of the TableLayout
to 0dp
but I don't want to do that. I specifically want to use wrap_content
as the layout_width
for the TableLayout
because using 0dp
instead of wrap_content
has an implication that I don't want.
Namely, when using 0dp
instead of wrap_content
, LinearLayout
will assume a width of 0 for the TableLayout
and then distribute the remaining space among all children. When using wrap_content
, however, LinearLayout
will first compute the minimum width of the child, subtract it from the remaining space, and then distribute the remaining space among the children, which will lead to quite different results which are important for my specific use case.
That's why I need to find a way to use wrap_content
with TableLayout
. When using a LinearLayout
instead of a TableLayout
, setting layout_width
to wrap_content
works fine. It's only TableLayout
that doesn't seem to be compatible with wrap_content
. Is this an Android bug or am I doing something wrong here?