Xamarin.Forms 中的元素未扩展

Gha*_*san 5 android xamarin.forms

使用StartAndExpandorEndAndExpand似乎实际上并没有扩展里面的元素StackLayout。即使有更多可用空间,如蓝色区域所示。只有FillAndExpand有效。

这是代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             xmlns:local="clr-namespace:App6"
             x:Class="App6.MainPage"
             ios:Page.UseSafeArea="True">

    <StackLayout BackgroundColor="Blue"
                 HeightRequest="100"
                 Orientation="Horizontal"
                 Spacing="0"
                 VerticalOptions="Start">
        <Label BackgroundColor="Red"
               HorizontalOptions="Start"
               Text="Hi" />
        <StackLayout BackgroundColor="Salmon"
                     HorizontalOptions="EndAndExpand"
                     Orientation="Vertical">
            <BoxView BackgroundColor="Red"
                     HeightRequest="30"
                     HorizontalOptions="End"
                     VerticalOptions="Center"
                     WidthRequest="30" />
        </StackLayout>
    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

这是我得到的:

在此处输入图片说明

最重要的结果是当FillAndExpand用于 child 时StackLayout,其次是EndAndExpandwhich 不扩展,最后是StartAndExpand, which 也不会扩展。

如果父元素有更多空间,不应该扩展元素吗?如果是的话,为什么那么StartAndExpandEndAndExpand不工作?

注意:这仅在 Android 上使用 Xamarin.Forms 版本 3.4.0.1008975 进行了测试。

Gha*_*san 5

参考@AndroDevil 评论,空间实际上分布在 StackLayout 的子元素之间,但它们无法填充它,除非它们的布局选项包含Fill. 它的工作方式或多或少类似于 FlexLayout 的选项之间的空间。

我刚刚向 StackLayout 添加了另一个元素,现在更清楚发生了什么。

这是代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             xmlns:local="clr-namespace:App6"
             x:Class="App6.MainPage"
             ios:Page.UseSafeArea="True">

    <StackLayout BackgroundColor="Blue"
                 HeightRequest="100"
                 Orientation="Horizontal"
                 Spacing="0"
                 VerticalOptions="Start">
        <Label BackgroundColor="Red"
               HorizontalOptions="Start"
               Text="Hi" />

        <Label BackgroundColor="Red"
               HorizontalOptions="EndAndExpand"
               Text="Bye" />

        <StackLayout BackgroundColor="Salmon"
                     HorizontalOptions="EndAndExpand"
                     Orientation="Vertical">
            <BoxView BackgroundColor="Red"
                     HeightRequest="30"
                     HorizontalOptions="End"
                     VerticalOptions="Center"
                     WidthRequest="30" />
        </StackLayout>
    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

这是我得到的:

在此处输入图片说明

所以Expand意思如下:

  • StartAndExpand: 给我更多空间,但把我放在左边。
  • CenterAndExpand: 给我更多的空间,但把我放在中间。
  • EndAndExpand: 给我更多空间,但把我放在右边。
  • FillAndExpand: 给我更多的空间,我会填满它。