我試圖從代碼中添加一些控件,而不是從.xml中添加。我使用的代碼如下:如何從代碼創建UI?
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mainLayout = FindViewById<LinearLayout>(Resource.Id.mainLayout);
Button test = new Button(Window.Context) { Text = "BLABLABLA" };
test.SetWidth(100);
test.SetHeight(100);
LinearLayout.LayoutParams _params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent, 0.0f);
_params.SetMargins(10, 10, 0, 0);
mainLayout.AddView(test, _params);
}
但是按鈕沒有出現在我的應用程序中。什麼可能是錯誤的?
更新:現在的作品!但我不明白我做了什麼,爲什麼它沒有運行過。但無論如何,謝謝大家。
你可以顯示你的'Resource.Layout.Main'嗎? – 2013-03-06 14:03:44