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);
};
}
什麼我做錯了,我怎麼能爲每個行我在我的代碼添加元素的重量?