2017-06-16 55 views
0

我正在研究一個新的基於xamarin的項目。我必須在代碼中動態地管理活動的元素數,但是我在設置元素的權重時遇到了問題。作爲我想要的佈局示例: activityXamarin在c中設置元素重量#

第一行是在axml中作爲示例。其他行是我通過代碼添加新項目的結果,儘管我在c#代碼中創建的標記等於axml行標記。

下面是C#代碼:

LinearLayout contentView = FindViewById<LinearLayout>(Resource.Id.ATMListContentLayout); 

     foreach (var item in BitcoinQeueryInfo.ATMList.OrderBy(x => x.id)) 
     { 
      LinearLayout row = new LinearLayout(this) 
      { 
       WeightSum = 100, 
       Orientation = Orientation.Horizontal 
      }; 
      LinearLayout.LayoutParams paramsRow = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent); 
      row.LayoutParameters = paramsRow; 

      TextView text = new TextView(this) 
      { 
       Text = item.id + " " + item.address, 
       TextSize = 20, 
      }; 
      text.SetTextColor(Color.ParseColor("#000000")); 
      LinearLayout.LayoutParams paramsText = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WrapContent); 
      paramsText.Weight = 70; 
      text.LayoutParameters = paramsText; 

      Button button = new Button(this) 
      { 
       Text = "Sell BTC", 
       TextSize = 20 
      }; 
      LinearLayout.LayoutParams paramsBtn = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WrapContent); 
      paramsBtn.Weight = 30; 
      text.LayoutParameters = paramsBtn; 
      button.SetBackgroundResource(Resource.Color.orange); 
      button.SetTextColor(Color.ParseColor("#000000")); 

      row.AddView(text); 
      row.AddView(button); 
      contentView.AddView(row); 

      button.Click += delegate 
      { 
       BitcoinQeueryInfo.selectedATMId = item.id; 
       var newInstance = new Intent(this, typeof(BTCAmountScreen)); 
       StartActivity(newInstance); 
      }; 
     } 

什麼我做錯了,我怎麼能爲每個行我在我的代碼添加元素的重量?

回答

1

WeightSumWeightfloat的值。

你必須選擇:

取出WeightSum = 100

從德docs

定義的最大重量之和。如果未指定,則總和由 加上所有孩子的layout_weight來計算。這可以用於 實例,通過 爲單個孩子提供50%的總可用空間,使其layout_weight爲0.5,並將weightSum設置爲1.0。


2.WeightSum = 1.0fparamsText.Weight = 0.7f;paramsBtn.Weight = 0.3f;