0
我正在研究一个新的基于xamarin的项目。我必须在代码中动态地管理活动的元素数,但是我在设置元素的权重时遇到了问题。作为我想要的布局示例: Xamarin在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);
};
}
什么我做错了,我怎么能为每个行我在我的代码添加元素的重量?