2012-04-13 7 views
0

私のアプリでは、線形レイアウトにタブローを動的に追加する必要があります。私はこのコードを書いています:TableLowをlinearlayoutに動的に追加するandroid

LinearLayout tabella = (LinearLayout) findViewById(R.id.tabella_contatori); 

    for(int i =0; i<array_list.size(); i++){ 
     TableRow row = new TableRow(getApplicationContext()); 
     row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

     TextView data = new TextView(getApplicationContext()); 
     data.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.2f)); 
     data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium); 
     data.setTextColor(Color.BLACK); 
     data.setBackgroundColor(Color.WHITE); 
     data.setPadding(2, 0, 0, 0); 
     data.setText("asd"); 

     row.addView(data); 
     tabella.addView(row); 
    } 
} 

しかし私は何もappensを開けません。 array_list.sizeが0より大きいかどうかを確認しています。 どうすればいいですか?あなたの活動に おかげで、マティア

+2

通常、テーブルレイアウトにタブローを追加します – njzk2

+0

あなたのアプリではどこでこれを行いますか? onCreate?たぶん、ボタンをクリックしますか? – Simon

+0

ボタンをクリックせずに作成する必要があります。 – pindol

答えて

3

問題は、あなたのTextViewレイアウトPARAMSです。型はLinearLayout.LayoutParamsではなくTableRow.LayoutParamsでなければなりません。 data.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

1

テイクテーブルのレイアウトとあなたのmain.xmlでこれを使用してみてください...

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

      <TextView android:text="Some Text"/> 

    </TableRow> 
</TableLayout> 

this.setContentView(R.layout.main); 

/* Find Tablelayout defined in main.xml */ 
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout); 
    /* Create a new row to be added. */ 
    TableRow tr = new TableRow(this); 
    tr.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      /* Create a TextView to be the row-content. */  

     TextView data = new TextView(getApplicationContext()); 
     data.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.2f)); 
     data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium); 
     data.setTextColor(Color.BLACK); 
     data.setBackgroundColor(Color.WHITE); 
     data.setPadding(2, 0, 0, 0); 
     data.setText("asd"); 

      /* Add TextView to row. */ 
      tr.addView(data); 
    /* Add row to TableLayout. */ 
    tl.addView(tr,new TableLayout.LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 
関連する問題