I have a TableLayout defined as a layout resource which I am trying to programmatically add to a vertical LinearLayout whose gravity is "center". This is the code I use:
LayoutInflater inflater = getLayoutInflater();
LinearLayout ll = (LinearLayout) findViewById(R.id.testLinearLayout);
TableLayout myTable = (TableLayout) inflater.inflate(R.layout.table2x2, null);
ll.addView(myTable);
myTable gets added but instead of being in the center, it's vertically centered and aligned to the left. If instead of adding it programmatically I hardcode the same TableLayout definition inside the LinearLayout the center gravity is respected. I know I must be missing something, but I can't figure it out.
LinearLayout:
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/testLinearLayout"
android:layout_weight="1"
android:gravity="center">
</LinearLayout>
It has a weight because it's part of an horizontal layout.
TableLayout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="\"
android:id="@+id/textView2x2_o"
android:gravity="center" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:id="@+id/textView2x2_c0"
android:gravity="center" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="1"
android:id="@+id/textView2x2_c1"
android:gravity="center" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:id="@+id/textView2x2_r0"
android:gravity="center" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="0"
android:id="@+id/textView2x2_0"
android:gravity="center" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="0"
android:id="@+id/textView2x2_1"
android:gravity="center" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:text="1"
android:id="@+id/textView2x2_c2"
android:gravity="center" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="0"
android:id="@+id/textView2x2_4"
android:gravity="center" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="0"
android:id="@+id/textView2x2_5"
android:gravity="center" />
</TableRow>
</TableLayout>