2017-05-07 14 views
0

Ich habe versucht, ein Tabellenlayout mit 2 Tasten nebeneinander zu erstellen. Ich habe gelesen, aber ich kann nicht scheinen, um es auf dem Bildschirm zu zeigen. das ist mein aktueller Code für das TableLayout.xamarin Erstellen einer Tabelle mit 2 Tasten in jeder Zeile

TableLayout ll = FindViewById<TableLayout>(Resource.Id.buttonLayout); 
     TableLayout llInner = new TableLayout(this); 
     TableLayout.LayoutParams lp = new TableLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent); 
     llInner.Orientation = Orientation.Horizontal; 
     llInner.LayoutParameters = lp; 
     llInner.WeightSum = 2; 
     ll.AddView(llInner); 
     var i = 0; 
     var row = new TableRow(this); 
     //TableRow.LayoutParams rowlayout = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent); 
     row.LayoutParameters = lp; 

     foreach (var series1 in series) 
     { 
      var b = new Button(this); 
      b.Text = series1.Series; 
      //lp = new TableLayout.LayoutParams(1, LinearLayout.LayoutParams.WrapContent); 
      b.LayoutParameters = lp; 
      row.AddView(b); 
      i =+ 1; 
      if (i > 1) 
      { 
       llInner.AddView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MatchParent, TableLayout.LayoutParams.WrapContent)); 
       row = new TableRow(this); 
       row.LayoutParameters = lp; 
       i = 0; 
      } 
     } 

irgendwelche Vorschläge?

Vielen Dank im Voraus,

Bjorn

+0

Hallo, warum versuchst du nicht, .axml Seiten zu verwenden. In Xamarin Design-Seite ist traditionelle und Xamarin Formen opmitise. Und sehr nützlich. –

+0

Die Anzahl der zu erstellenden Schaltflächen ist dynamisch, da sie auf Informationen basiert, die ich von einer API erhalte. Daher weiß ich nicht, wie viele Tasten ich erstellen muss. –

Antwort

1

Ich habe versucht, nebeneinander ein Tabellenlayout mit 2 Tasten zu erstellen.

einfach aus dem Code neben der ursprünglichen TableLayoutbuttonLayout genannt, erstellt Sie einen anderen TableLayout und TableRow und fügte dann Button Kontrollen in diese Reihe, aber man kann nie auf die Ansicht Ihrer TableLayout diese Zeile hinzufügen, und Sie müssen Sie auf die LayoutParams achten, verwendeten Sie eine TableLayout.LayoutParams für alle Steuerelemente, aber im Inneren ist die LinearLayout.LayoutParams, das ist nicht richtig.

Da ich nicht Ihre series haben, kann ich nicht wirklich verstehen, was Sie in foreach tust, hier poste ich nur ein Beispielcode:

TableLayout ll = FindViewById<TableLayout>(Resource.Id.buttonLayout); 

ll.WeightSum = 2; 
ll.Orientation = Orientation.Horizontal; 

TableRow row = new TableRow(this); 
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent); 
row.LayoutParameters = lp; 

var b = new Button(this); 
b.Text = "Button1"; 
b.LayoutParameters = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent); 
row.AddView(b); 

var bb = new Button(this); 
bb.Text = "Button2"; 
bb.LayoutParameters = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent); 
row.AddView(bb); 

ll.AddView(row); 
ll.Invalidate(); 
0

ich war zuzuteilen Sachen zu tun, die wasn Ich brauche es nicht. aber mein dümmster Fehler war der Teil, wo ich i = + 1 gemacht habe; anstelle von i + = 1;

so sieht mein Code jetzt aus. es tut was es jetzt tun muss.

TableLayout ll = FindViewById<TableLayout>(Resource.Id.buttonLayout); 
      //TableLayout llInner = new TableLayout(this); 
      TableRow.LayoutParams lp = new TableRow.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent); 
      //ll.Orientation = Orientation.Horizontal; 
      //ll.LayoutParameters = lp; 
      //ll.WeightSum = 2; 
      //ll.AddView(llInner); 
      var i = 0; 
      var row = new TableRow(this); 
      row.WeightSum = 2; 
      //TableRow.LayoutParams rowlayout = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent); 
      //row.LayoutParameters = lp; 

      foreach (var series1 in series) 
      { 
       var b = new Button(this); 
       b.Text = series1.Series; 
       lp.Weight = 1; 
       //lp = new TableLayout.LayoutParams(1, LinearLayout.LayoutParams.WrapContent); 
       b.LayoutParameters = lp; 
       row.AddView(b); 
       i += 1; 
       if (i > 1) 
       { 
        ll.AddView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MatchParent, TableLayout.LayoutParams.WrapContent)); 
        row = new TableRow(this); 
        row.LayoutParameters = lp; 
        i = 0; 
       } 

allot von stuff ist kommentiert, weil ich seit geraumer zeit damit rumgespielt habe.

Verwandte Themen