我试图从代码中添加一些控件,而不是从.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