2017-06-16 49 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;