提问者:小点点

用StackLayout填充所有屏幕


我有一个StackLayout代码如下:

StackLayout mainStackLayOut = new StackLayout{
    BackgroundColor = Color.Blue,
    //VerticalOptions = LayoutOptions.FillAndExpand,
    //WidthRequest = width,
    HorizontalOptions = LayoutOptions.FillAndExpand,
    Orientation = StackOrientation.Vertical
};

但是我希望堆栈布局填满所有的屏幕宽度和高度,我还添加了树按钮,如下所示:

StackLayout buttonsStackLayOut = new StackLayout
{
    BackgroundColor = Color.White,
    //VerticalOptions = LayoutOptions.Fill,
    HorizontalOptions = LayoutOptions.Fill,
    Orientation = StackOrientation.Horizontal,
    Spacing = 0
};
mainStackLayOut.Children.Add(buttonsStackLayOut);


Image doctorImage = new Image
{
    WidthRequest = width / 3,
    HeightRequest = 50,
    BackgroundColor = Color.Gray,
    Source = ImageSource.FromFile ("about.png")
};
buttonsStackLayOut.Children.Add(doctorImage);

我怎样才能填满所有的屏幕尺寸?


共1个答案

匿名用户

以最简单的形式,我已经设法达到了你所要求的。

我将屏幕的内容设置为主堆栈布局。

        var mainStackLayout = new StackLayout { BackgroundColor = Color.Blue};
        var buttonsStackLayout = new StackLayout { BackgroundColor = Color.White, Orientation = StackOrientation.Horizontal , Spacing = 0 };

        Image about = new Image { HeightRequest = 50, Source = ImageSource.FromFile("about.jpg") };
        Image twitter = new Image { HeightRequest = 50, Source = ImageSource.FromFile("twitter.jpg") };
        Image refresh = new Image { HeightRequest = 50, Source = ImageSource.FromFile("refresh.jpg") };

        buttonsStackLayout.Children.Add(about);
        buttonsStackLayout.Children.Add(twitter);
        buttonsStackLayout.Children.Add(refresh);

        mainStackLayout.Children.Add(buttonsStackLayout);
        this.Content = mainStackLayout;