2017-05-07 2 views
0

나는 서로 옆에 2 개의 버튼이있는 표 레이아웃을 만들려고했습니다. 나는 주변에서 읽었지만, 나는 화면에 아무 것도 보여줄 수 없다. 이것은 TableLayout에 대한 현재 코드입니다.xamarin 각 행에 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

생성해야하는 버튼의 양이 동적 인 문제가 있습니다. 이는 API에서받은 정보를 기반으로합니다. 그러므로 나는 얼마나 많은 버튼을 만들어야할지 모른다. –

답변

1

은 내가 서로 옆에이 개 버튼이있는 표 레이아웃을 만들기 위해 노력했습니다. 원래 TableLayoutbuttonLayout라는 이름 외에 그냥 코드에서

는 다른 TableLayoutTableRow을 생성 한 다음이 행에 Button 컨트롤을 추가,하지만 당신은 당신에게 당신의 TableLayout의보기에이 행을 추가하지, 결코 LayoutParams에주의를 기울여야하고, 모든 컨트롤에 TableLayout.LayoutParams을 사용했지만 그 안에는 LinearLayout.LayoutParams이 적합하지 않습니다. 내가 물건을 할당한다하고 있었다

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

년후 : 나는 당신의 series을 가지고 있지 않기 때문에

, 난 정말 여기 난 그냥 게시, 당신은 foreach에서 무슨 일을하는지 샘플 코드를 이해할 수 없다 필요 없어. 그러나 나의 어리석은 실수는 내가했던 부분이었습니다. 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; 
       } 

내가 잠시 동안 주변을 어지럽 혔기 때문에 많은 것들이 주석 처리되었습니다.

관련 문제