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?
Se puede publicar su solución de código? Parece que ya estás haciendo eso arriba. – Atma
¡Gracias! @Atma TableRows debe usar TableLayout.LayoutParams y las vistas dentro de TableRows deben usar TableRow.LayoutParams. :) – Brayden
Horrible que necesita hacer esto explícitamente, pero funcionó para mí, ¡gracias! –