J'essaie de créer un TableLayout par programmation. Ça ne marchera pas. La même disposition dans un fichier XML fonctionne bien. Voici ce que j'ai:Créer TableLayout par programme
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);
Lorsque je lance ceci, je n'obtiens pas de plantage, mais rien ne s'affiche. Si je me débarrasse de l'instance de TableRow, au moins le bouton apparaît comme un enfant direct de TableLayout. Qu'est-ce que je fais mal?
Pouvez-vous poster votre solution de code? Il semble que vous le fassiez déjà ci-dessus. – Atma
Merci! @Atma TableRows doit utiliser TableLayout.LayoutParams et les vues à l'intérieur de TableRows doivent utiliser TableRow.LayoutParams. :) – Brayden
Horrible que vous avez besoin de faire explicitement cela, mais cela a fonctionné pour moi, merci! –