2011-09-24 9 views
5

He visto muchas publicaciones relacionadas con la adición dinámica de filas de tablas, pero no estoy seguro de lo que me falta.Filas de tablas añadidas dinámicamente que no aparecen

Cuando ejecuto lo siguiente, no se muestra nada (aparte de la barra de título de la aplicación).

Mi Disposición:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     android:id="@+id/table_view_test_main" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     > 
    <ScrollView 
      android:id="@+id/tvt_scroll" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      > 
     <RelativeLayout 
       android:id="@+id/tvt_scroll_relative" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
      <TableLayout 
        android:id="@+id/tvt_tableview" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        > 
      </TableLayout> 
     </RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 

mi actividad:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.table_view); 

    TableLayout tableLayout = (TableLayout) findViewById(R.id.tvt_tableview); 

    TableRow tableRow = new TableRow(this); 
    tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 

    TextView column1 = new TextView(this); 
    column1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    column1.setBackgroundColor(Color.YELLOW); 
    column1.setTextColor(Color.BLUE); 
    column1.setText("Col1 Value"); 
    tableRow.addView(column1); 

    TextView column2 = new TextView(this); 
    column2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    column2.setBackgroundColor(Color.RED); 
    column2.setTextColor(Color.GREEN); 
    column2.setText("Col2 Value"); 
    tableRow.addView(column2); 

    tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); 

    //tl.refreshDrawableState(); 
    //findViewById(R.id.tvt_scroll_relative).refreshDrawableState(); 
} 
+0

interesante. Todo esto se ve bien para mí (no es que esté absolutamente seguro de nada aquí ...). Recomiendo tratar de agregar dinámicamente otros 'TextView' a los otros diseños por encima de' TableLayout'. Vea si puede obtener * algo * para aparecer. Haganos saber como funciona. –

+0

eliminar la línea tableRow.setLayoutParams(); después de haber instanciado TableRow, debería funcionar, creo. Avíseme si esto funciona, para poder publicarlo como una solución. –

+0

@Yashwanth Kumar, eliminar eso solo no ayudó. Cambiar ViewGroup.LayoutParams a TableRow.LayoutParams o eliminarlos por completo resuelve el problema. – CrackerJack9

Respuesta

11

Cambiar las dos referencias

ViewGroup.LayoutParams 

a

TableRow.LayoutParams

y debería funcionar.

Agregar parámetros de diseño a un widget requiere que el LayoutParams sea una clase interna del contenedor de diseño circundante.

+0

Gracias por la respuesta, ¿tiene alguna referencia para usar la clase interna del contenedor de diseño circundante? – CrackerJack9

+0

Tuve el mismo problema y lo descubrí después de experimentar. También me interesaría alguna referencia. –

1

Creo que esto tiene algo que ver con la configuración de los parámetros de diseño en el TextView s. Si elimina esas definiciones, aparece la información. Además, si usted toma un vistazo a la documentación oficial TableRow se puede ver:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

+0

No es del todo cierto, ya que puede establecer 'TableRow.LayoutParams.span', en cuyo caso deberá establecer' LayoutParam's. – CrackerJack9

Cuestiones relacionadas