2017-05-07 90 views
0

我一直在尝试创建一个表格布局,旁边有两个按钮。我一直在阅读,但我似乎无法让它显示任何东西在屏幕上。这是我当前的TableLayout代码。xamarin创建一个tablelaylay与每个行上的2个按钮

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

有什么建议?

在此先感谢,

比约恩

+0

嗨,那你为什么不尝试使用.axml页面。在Xamarin传统和Xamarin形式设计页面是opmitise。而且非常有用。 –

+0

sinds需要创建的按钮数量是动态的,它基于我从API接收到的信息。因此我不知道我需要创建多少个按钮。 –

回答

1

我一直在试图建立一个表布局,2个按钮旁边的海誓山盟。

刚刚从你的代码中,除了原有的TableLayout命名为buttonLayout,你创造了另一个TableLayoutTableRow,然后加入Button控制到该行,但你永远不此行添加到您的TableLayout的观点,和你需要注意LayoutParams,所有控件都使用了TableLayout.LayoutParams,但其内部是LinearLayout.LayoutParams,这是不对的。

因为我没有你series做,我真的不能明白你在foreach做什么,在这里我只是张贴的示例代码:

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

我在做配发的东西,WASN不需要。但是我最愚蠢的错误是我做了我= 1的部分;而不是i + = 1;

这就是我的代码现在的样子。它正在做它现在需要做的笏。

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

分配的东西被评论,因为我已经搞乱了一段时间了。

相关问题