我正在尝试以编程方式将行添加到以XML定义的表中(此行中包含内部表)。
<TableLayout android:id="@+id/maintable"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="*" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="@+id/tabrow">
<TableLayout android:id="@+id/tableLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<TextView android:id="@+id/titlerow" android:layout_height="fill_parent" android:text="Hello world"
android:gravity="center" android:layout_width="fill_parent"
android:layout_weight="1" android:textSize="17sp"></TextView>
</TableRow>
<TableRow>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="@string/time1" android:id="@+id/time1" style="@style/timeview"></TextView>
<TextView android:text="@string/time2" style="@style/dayview"></TextView>
<TextView android:text="@string/time3" style="@style/dayview"></TextView>
</LinearLayout>
</TableRow>
</TableLayout>
</TableRow>
现在我想在表格中多次添加此(tabrow)行。
我该怎么办?
答案 0 :(得分:2)
这样的事情应该做:
// get your table layout
TableLayout tl = (TableLayout) findViewById(R.id.WhateverYoursIs);
// Create new row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// Create textview
TextView t = new TextView(this);
//
// configure your textview's and do this 2 more times
//
t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// add textview to row
tr.addView(t);
//
// do this 2 more times
//
// add row to table
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
你必须根据你需要的频率将行创建内容放入循环中