2009-10-07 10 views
23

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?

Répondre

6

Il s'avère que j'avais besoin de spécifier TableRowLayout, TableLayout etc pour les paramètres de mise en page, sinon la table ne s'affichera pas!

+2

Pouvez-vous poster votre solution de code? Il semble que vous le fassiez déjà ci-dessus. – Atma

+3

Merci! @Atma TableRows doit utiliser TableLayout.LayoutParams et les vues à l'intérieur de TableRows doivent utiliser TableRow.LayoutParams. :) – Brayden

+0

Horrible que vous avez besoin de faire explicitement cela, mais cela a fonctionné pour moi, merci! –

0

Pour moi, pour obtenir le mien, j'ai dû appeler le addContentView().

29

Juste pour rendre la réponse plus claire:

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); 

Explication
Lorsque vous appelez setLayoutParams, vous êtes censé passer le LayoutParams de la vue parent

+3

Ne devrait-il pas s'agir de tableRow.setLayoutParams (rowParams)? – Leon

+0

Le prix Life Saver va à @Gigori A. et marque. Gigori A. pour cette réponse et marque pour sa propre réponse. –

+0

Solution géniale et utile. En remarque, vous devez ajouter la ligne à la table avant d'ajouter la textview à la ligne. tableLayout.addView (tableRow); –

0

Votre problème est à cette ligne:

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

Vous devez changer LayoutParams-TableRow.LayoutParams:

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