如何使用 xamarin 以编程方式制作按钮? [英] How do you make buttons programmatically using xamarin?

查看:52
本文介绍了如何使用 xamarin 以编程方式制作按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 xamarin ide (c#) 以编程方式创建按钮.我需要什么代码来创建按钮、设置其大小、设置其文本、设置其背景颜色和设置其约束?有没有办法将按钮定义为屏幕宽度的 1/4?提前致谢.

I'm trying to create buttons programmatically using the xamarin ide (c#). What code do I need to create the button, set its size, set its text, set its background color, and set its constraints? Is there a way to define the button as 1/4 of the width of the screen? Thanks in advance.

推荐答案

先创建按钮

UIButton button = new UIButton(); 
CGRect ScreenBounds = UIScreen.MainScreen.Bounds;
float buttonWidth = (float)ScreenBounds.X / 4;
button.Frame = new CGRect (0f, 0f, buttonWidth, 50f); 
button.SetTitle ("Title", UIControlState.Normal);
button.BackgroundColor = UIColor.Green;

然后将其作为子视图添加到活动视图中

And then add it as a subview to the active view

this.AddSubview (button);

并为 TouchUpInside

button.TouchUpInside += (object sender, System.EventArgs e) => {
   Debug.WriteLine( "Button Clicked!");
};

这篇关于如何使用 xamarin 以编程方式制作按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆