2009-10-07 8 views
23

Ich versuche, ein TableLayout programmgesteuert zu erstellen. Es wird einfach nicht funktionieren. Das gleiche Layout in einer XML-Datei funktioniert jedoch. Dies ist, was ich habe:Erstellen Sie TableLayout programmgesteuert

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

Wenn ich dies ausführen, bekomme ich keinen Absturz, aber nichts erscheint. Wenn ich die TableRow-Instanz loswerde, erscheint zumindest die Schaltfläche als direktes Kind der TableLayout-Instanz. Was mache ich falsch?

Antwort

6

Es stellt sich heraus, dass ich TableRowLayout, TableLayout usw. für die Layout-Parameter angeben musste, sonst wird die Tabelle einfach nicht angezeigt!

+2

können Sie Ihren Code Lösung vorschlagen? Es sieht so aus, als ob du das schon oben machst. – Atma

+3

Danke! @Atma TableRows sollte TableLayout.LayoutParams verwenden und Ansichten innerhalb der TableRows sollten TableRow.LayoutParams verwenden. :) – Brayden

+0

Schrecklich, dass Sie dies explizit tun müssen, aber es hat für mich funktioniert, danke! –

0

Für mich, um meine zu bekommen, musste ich addContentView() anrufen.

29

Nur die Antwort klarer zu machen:

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

Erklärung
Wenn Sie setLayoutParams aufrufe, werden Sie die LayoutParams der übergeordnete Ansicht

+3

Sollte es nicht tableRow.setLayoutParams (rowParams) sein? – Leon

+0

Der Lebensretterpreis geht @Gigori A. und Markierung. Gigori A. für diese Antwort und markieren für seine eigene Antwort. –

+0

Große und hilfreiche Lösung. Als Randnotiz sollten Sie die Zeile der Tabelle hinzufügen, bevor Sie die Textansicht zur Zeile hinzufügen. tableLayout.addView (tableRow); –

0

Ihr Problem an dieser Linie ist:

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

Sie müssen sich ändern LayoutParams-TableRow.LayoutParams:

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