2009-10-07 18 views
23

Estoy tratando de crear un TableLayout programáticamente. Simplemente no funcionará. Sin embargo, funciona el mismo diseño en un archivo xml. Esto es lo que tengo:Crear TableLayout programmatically

public class MyTable extends TableLayout 
{ 
    public MyTable(Context context) { 
     super(context); 

     setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     TableRow row = new TableRow(context); 
     row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

     Button b = new Button(getContext()); 
     b.setText("hello"); 
     b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
     row.addView(b); 
     addView(row) 
    } 
} 

... 

// In main activity: 
MyTable table = new MyTable(this); 
mainLayout.addView(table); 

Cuando ejecuto esto, no aparece un bloqueo, pero no aparece nada. Si elimino la instancia de TableRow, al menos el botón aparece como un elemento directo de TableLayout. ¿Qué estoy haciendo mal?

Respuesta

6

Resulta que necesitaba especificar TableRowLayout, TableLayout, etc. para los parámetros de diseño, de lo contrario, ¡la tabla simplemente no se mostrará!

+2

Se puede publicar su solución de código? Parece que ya estás haciendo eso arriba. – Atma

+3

¡Gracias! @Atma TableRows debe usar TableLayout.LayoutParams y las vistas dentro de TableRows deben usar TableRow.LayoutParams. :) – Brayden

+0

Horrible que necesita hacer esto explícitamente, pero funcionó para mí, ¡gracias! –

0

Para mí, para obtener la mía tuve que llamar al addContentView().

29

sólo para hacer la respuesta más clara:

TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT); 
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 

TableLayout tableLayout = new TableLayout(context); 
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));// assuming the parent view is a LinearLayout 

TableRow tableRow = new TableRow(context); 
tableRow.setLayoutParams(tableParams);// TableLayout is the parent view 

TextView textView = new TextView(context); 
textView.setLayoutParams(rowParams);// TableRow is the parent view 

tableRow.addView(textView); 

Explicación
Cuando se llama a setLayoutParams, se supone que para pasar el LayoutParams de la vista padre

+3

¿No debería ser tableRow.setLayoutParams (rowParams)? – Leon

+0

El premio de salvavidas va para @Gigori A. y marca. Gigori A. por esta respuesta y marca su propia respuesta. –

+0

Solución genial y útil. Como nota al margen, debe agregar la fila a la tabla antes de agregar la vista de texto a la fila. tableLayout.addView (tableRow); –

0

Su problema está en esta línea:

b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 

Es necesario cambiar LayoutParams a TableRow.LayoutParams:

b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));