我正在尝试将最后两个按钮对齐(并排),这在XAML中要容易得多,如何使用代码方法实现这一目标?
MainPage = new ContentPage
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Spacing = 40,
Children = {
new Label {
XAlign = TextAlignment.Center,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = "Hey there here is where you spent your time!"
},
new Label()
{
XAlign = TextAlignment.Center,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = "Time spent near beacon A: "
},
new Label()
{
XAlign = TextAlignment.Center,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = "Time spent near beacon B: "
},
new Button()
{
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Start,
Text = "Start Tracking"
},
new Button()
{
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Start,
Text = "Stop Tracking"
}
}
}
};
感谢您的任何意见。
答案 0 :(得分:5)
我得到了一些像这样的StackLayout启动:
MainPage = new ContentPage
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Spacing = 40,
Children = {
new Label {
XAlign = TextAlignment.Center,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = "Hey there here is where you spent your time!"
},
new Label()
{
XAlign = TextAlignment.Center,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = "Time spent near beacon A: "
},
new Label()
{
XAlign = TextAlignment.Center,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = "Time spent near beacon B: "
},
new StackLayout()
{
HorizontalOptions = LayoutOptions.Center,
Orientation = StackOrientation.Horizontal,
Children = {
new Button()
{
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Start,
Text = "Start Tracking"
},
new Button()
{
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Start,
Text = "Stop Tracking"
}
}
}
}
}
};