如何使用C#将标签放置在网格的顶部66%

时间:2018-11-20 13:13:47

标签: xamarin xamarin.forms

我正在使用此C#代码创建一个内部带有Label的网格,然后将其添加到details元素:

details.Children.Clear();
var grid = new Grid()
{
    VerticalOptions = LayoutOptions.FillAndExpand,
    Margin = new Thickness(60, 20, 60, 20)
};
grid.Children.Add(new Label
{
    Text = msg,
    VerticalOptions = LayoutOptions.Center,
    HorizontalOptions = LayoutOptions.Center,
    TextColor = (Color) Application.Current.Resources["FooterTextColor"],
    Style = (Style)footerText
});
details.Children.Add(grid);

这可行,但是我想拥有的标签不在中心(如现在)中心,而是在网格顶部2/3的中心。

有人可以给我一些建议吗?

1 个答案:

答案 0 :(得分:0)

您需要将RowDefinitions添加到Grid

grid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (2, GridUnitType.Star) });
grid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) });

请注意GridLength:它将第一行和第二行之间的栅格高度2:1分开。

然后您可以将标签分配给第一行:

Label myLabel = new Label
{
    Text = msg,
    VerticalOptions = LayoutOptions.Center,
    HorizontalOptions = LayoutOptions.Center,
    TextColor = (Color) Application.Current.Resources["FooterTextColor"],
    Style = (Style)footerText
};

grid.Children.Add(myLabel, 0, 0); // Split (view, row, column)