我尝试以编程方式向TableLayout添加行,但不显示它们。仅显示在布局上创建的表的标题。
我的TableLayout:
<TableLayout
android:id="@+id/resultTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#000"
android:stretchColumns="1">
<TableRow android:padding="1dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:gravity="center_horizontal"
android:text="№"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:background="#cccccc"
android:gravity="center_horizontal"
android:text="Вопрос"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:background="#cccccc"
android:gravity="center_horizontal"
android:text="Результат"
android:textStyle="bold" />
</TableRow>
</TableLayout>
代码:
TableLayout resultTable=(TableLayout)findViewById(R.id.resultTable);
int i=1;
for (Question q:questions)
{
resultTable.addView(buildTableRow(""+i,q.getText(),resultExamMap.get(q.getId())));
i++;
}
这些方法创建了TableRow,我在TableLayout中添加了它:
private TextView buildTableColumn(String text,int color)
{
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(1,0,0,0);
TextView column=new TextView(this);
column.setBackgroundColor(color);
column.setText(text);
return column;
}
private TableRow buildTableRow(String textCol1,String textCol2,boolean isTrue)
{
TableRow tableRow=new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
tableRow.addView(buildTableColumn(textCol1,0xcccccc));
tableRow.addView(buildTableColumn(textCol2,0xcccccc));
if(isTrue) {
tableRow.addView(buildTableColumn("True",0x00ff00));
}
else{
tableRow.addView(buildTableColumn("False",0xff0000));
}
return tableRow;
}
感谢。