我在xml中创建了一个TableLayout。现在我想在我的活动'onCreate()中以编程方式添加表行。方法fillTable应该完成这项工作,但TableRow不会显示在表中。
这是我的代码:
在活动中:
public void fillTable() {
t1 = (TableLayout) findViewById(R.id.tabelle);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
TextView rang = new TextView(this);
TextView mannschaftsname = new TextView(this);
TextView spiele = new TextView(this);
TextView siege = new TextView(this);
TextView unentschieden = new TextView(this);
TextView niederlagen = new TextView(this);
TextView tore = new TextView(this);
TextView doppelpunkt = new TextView(this);
TextView gegentore = new TextView(this);
TextView differenz = new TextView(this);
TextView punkte = new TextView(this);
rang.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
rang.setGravity(Gravity.CENTER_HORIZONTAL);
rang.setPadding(1, 1, 1, 1);
rang.setText("0");
rang.setTextColor(R.color.white);
mannschaftsname.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
mannschaftsname.setGravity(Gravity.CENTER_HORIZONTAL);
mannschaftsname.setPadding(1, 1, 1, 1);
mannschaftsname.setText("TEST");
mannschaftsname.setTextColor(R.color.white);
spiele.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
spiele.setGravity(Gravity.CENTER_HORIZONTAL);
spiele.setPadding(1, 1, 1, 1);
spiele.setText("0");
spiele.setTextColor(R.color.white);
siege.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
siege.setGravity(Gravity.CENTER_HORIZONTAL);
siege.setPadding(1, 1, 1, 1);
siege.setText("0");
siege.setTextColor(R.color.white);
unentschieden.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
unentschieden.setGravity(Gravity.CENTER_HORIZONTAL);
unentschieden.setPadding(1, 1, 1, 1);
unentschieden.setText("0");
unentschieden.setTextColor(R.color.white);
niederlagen.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
niederlagen.setGravity(Gravity.CENTER_HORIZONTAL);
niederlagen.setPadding(1, 1, 1, 1);
niederlagen.setText("0");
niederlagen.setTextColor(R.color.white);
tore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tore.setGravity(Gravity.CENTER_HORIZONTAL);
tore.setPadding(1, 1, 1, 1);
tore.setText("0");
tore.setTextColor(R.color.white);
doppelpunkt.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
doppelpunkt.setGravity(Gravity.CENTER_HORIZONTAL);
doppelpunkt.setPadding(1, 1, 1, 1);
doppelpunkt.setText(":");
doppelpunkt.setTextColor(R.color.white);
gegentore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
gegentore.setGravity(Gravity.CENTER_HORIZONTAL);
gegentore.setPadding(1, 1, 1, 1);
gegentore.setText("0");
gegentore.setTextColor(R.color.white);
differenz.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
differenz.setGravity(Gravity.CENTER_HORIZONTAL);
differenz.setPadding(1, 1, 1, 1);
differenz.setText("0");
differenz.setTextColor(R.color.white);
punkte.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
punkte.setGravity(Gravity.CENTER_HORIZONTAL);
punkte.setPadding(1, 1, 1, 1);
punkte.setText("0");
punkte.setTextColor(R.color.white);
tr.addView(rang);
tr.addView(mannschaftsname);
tr.addView(spiele);
tr.addView(siege);
tr.addView(unentschieden);
tr.addView(niederlagen);
tr.addView(tore);
tr.addView(doppelpunkt);
tr.addView(gegentore);
tr.addView(differenz);
tr.addView(punkte);
t1.addView(tr, new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
show_table.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabelle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tablle_tableRow1"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_span="9"
android:text="Liganame"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
</TableRow>
<TableRow
android:id="@+id/tabelle_tableRow2"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="1dp" >
<TextView
android:id="@+id/textView_rang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="1dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_mannschaft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_spiele"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="Sp."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_siege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="S"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_unentschieden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="U"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_niederlagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="1dp"
android:text="N"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_tore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_span="3"
android:gravity="right"
android:padding="1dp"
android:text="Tore"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_differenz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="Diff."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_punkte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="Pkt."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
</TableRow>
<TableRow
android:id="@+id/tabelle_tableRow3"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/rang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="1dp"
android:text="0"
android:textColor="@color/white" />
<TextView
android:id="@+id/mannschaft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="1dp"
android:text="BBBBBBBBBB"
android:textColor="@color/white" />
<TextView
android:id="@+id/spiele"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />
<TextView
android:id="@+id/siege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />
<TextView
android:id="@+id/unentschieden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />
<TextView
android:id="@+id/niederlagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="99"
android:textColor="@color/white" />
<TextView
android:id="@+id/tore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />
<TextView
android:id="@+id/doppelpunkt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text=":"
android:textColor="@color/white" />
<TextView
android:id="@+id/gegentore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />
<TextView
android:id="@+id/differenz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />
<TextView
android:id="@+id/punkte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="right"
android:padding="1dp"
android:text="999"
android:textColor="@color/white" />
</TableRow>
答案 0 :(得分:2)
rang.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
mannschaftsname.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
spiele.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
siege.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
unentschieden.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
niederlagen.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
doppelpunkt.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
gegentore.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
differenz.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
punkte.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
请删除上面提到的代码行,在执行此操作时我也遇到了问题,当我删除这些行时,表格是动态显示的... 如果有效,请回复。
尝试一下它会起作用......