使用StackLayout填充所有屏幕

时间:2014-06-23 00:17:53

标签: xamarin xamarin.forms

我的StackLayout编码如下:

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

但是我希望StackLayout能够填满所有的屏幕宽度和高度,我还添加了这样的树状按钮:

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);

enter image description here

如何填写所有屏幕尺寸?

1 个答案:

答案 0 :(得分:9)

以最简单的形式,我设法达到了你所需要的目标。

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

        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; 

enter image description here