2012-04-13 2 views
0

Мне нужно динамически добавлять некоторые tablerows в linearlayout в моем приложении. Я пишу этот код:Добавить TableRow динамически в 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); 
    } 
} 

Но когда я открываю приложение, ничего не появляется. Я уже проверяю, больше ли array_list.size, чем 0. Как я могу это сделать? Спасибо, Маттиа

+2

Вы обычно добавляете таблетки в табличные лотки – njzk2

+0

Куда вы в это приложение? OnCreate? Может быть, нажатие кнопки? – Simon

+0

Мне нужно сделать это без oncreate, не нажимая на кнопку – pindol

ответ

3

Проблема заключается в вашей TextView Layout Params. Тип должен быть TableRow.LayoutParams не LinearLayout.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)); 
Смежные вопросы