я совершенно не понимаю, почему test, test1, test2, test3 неправильно выравниваются с первой строкой. Первая строка относится к тесту, 2012, тесту, тесту. Я хочу, чтобы следующая строка следовала за первой строкой в таблице. Пожалуйста помоги. Заранее спасибо.
XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tl"
android:weightSum="5"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#27160e" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textStyle="bold"
android:textSize="18sp"
android:gravity="center"
android:textColor="#fad06e"
android:layout_weight="1"
android:text="Project" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textStyle="bold"
android:textSize="18sp"
android:gravity="center"
android:textColor="#fad06e"
android:layout_weight="1"
android:text="Project2" />
<TableLayout
android:layout_weight="3"
android:layout_marginRight="4dp"
android:stretchColumns="*"
>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#fad06e"
android:text="Cost"
android:gravity="center"
android:layout_span="2" />
</TableRow>
<TableRow
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#fad06e"
android:text="2011" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#fad06e"
android:text="2012" />
</TableRow>
</TableLayout>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="test" />
<TableLayout android:stretchColumns="*"
android:layout_weight="3">
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
Код:
TableLayout tl;
tl = (TableLayout) findViewById(R.id.tl);
for(int x = 0; x < 3; x++) {
// Create the table row
TableRow tr = new TableRow(this);
tr.setId(100+x);
TextView lblrecID = new TextView(this);
lblrecID.setId(200+x);
lblrecID.setText("test");
lblrecID.setGravity(Gravity.CENTER);
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
lblrecID.setLayoutParams(params);
tr.addView(lblrecID);// add the column to the table row here
TextView lblrecName = new TextView(this);
lblrecName.setId(200+x);// define id that must be unique
lblrecName.setText("test1");
lblrecName.setGravity(Gravity.CENTER);
lblrecName.setLayoutParams(params);
tr.addView(lblrecName); // add the column to the table row here
TableLayout tl1 = new TableLayout(this);
LayoutParams params1 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
tl1.setStretchAllColumns(true);
tl1.setLayoutParams(params1);
TableRow tr2 = new TableRow(this);
TextView lblrecID1 = new TextView(this);
lblrecID1.setId(200+x);
lblrecID1.setText("test2");
lblrecID1.setGravity(Gravity.CENTER);
tr2.addView(lblrecID1);// add the column to the table row here
TextView lblrecName1 = new TextView(this);
lblrecName1.setId(200+x);// define id that must be unique
lblrecName1.setText("test3");
lblrecName1.setGravity(Gravity.CENTER);
tr2.addView(lblrecName1); // add the column to the table row here
tl1.addView(tr2);
// finally add this to the table row
tr.addView(tl1);
//tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
Результат