2013-01-15 83 views
2

我已經在xml中創建了一個TableLayout。現在我想以編程方式在我的活動onCreate()中添加一個表格行。方法fillTable應該完成這項工作,但TableRow不會顯示在表中。編程添加的tableRow不會顯示在TableLayout中

這是我的代碼:

在活動:

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

貴XML的佈局被誇大,添加行只會工作,如果佈局已經膨脹之前 – Nickolaus

+0

如果你要添加到TableLayout編程,我不認爲你不需要將它們添加到XML佈局中。 –

+0

正確的XML佈局是靜態的,以編程方式構建佈局是動態的並且沒有連接到XML – Nickolaus

回答

1
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)); 

請刪除的代碼上述線,我也有親當我這樣做時,當我刪除這些線表動態顯示... 答覆,如果這個工程。

試試這個它會工作...

相關問題