Android のテーブル レイアウトに行を動的に追加しようとしています (このチュートリアルを参照しました: http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout )。ただし、これは間違ったビュー階層につながっています。コードと出力画像を添付しました。
XML ファイル:
<TableLayout
android:id="@+id/task_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:id="@+id/task_heading_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/task_heading_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="Task"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="1"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="2"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="3"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="4"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="5"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="6"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="7"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
<TextView
android:id="@+id/time_8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="6"
android:text="8"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" >
</TextView>
</TableRow>
</TableLayout>
Java コード:
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView t1 = new TextView(this);
t1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
String heading = "heading";
t1.setText(heading);
row.addView(t1);
for (int i=0;i<8;i++){
TextView view = new TextView(this);
view.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
view.setText("child");
row.addView(view);
}
task_table.addView(row, new TableLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
出力では、ループに追加しているテキスト「子」を含む8つのビューすべてが、列ごとに1つの「子」ではなく、1というタイトルの列に追加されます。
期待される出力:
Task | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
heading | child | child | child | child | child | child | child | child
実際の出力:
Task | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
heading | child child child child child child child child | | | | | | |
誰かが私を助けて、私が間違っていることを教えてもらえますか? どんな助けでも大歓迎です。