【问题标题】:Element not expanding in Xamarin.Forms元素未在 Xamarin.Forms 中扩展
【发布时间】:2019-09-11 07:37:41
【问题描述】:

使用StartAndExpandEndAndExpand 似乎并没有真正扩展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>

这是我得到的:

当对子 StackLayout 使用 FillAndExpand 时,最重要的结果是不展开的 EndAndExpand,最后是同样不展开的 StartAndExpand

如果父元素有更多空间,难道不应该扩展元素吗?如果是,那为什么StartAndExpandEndAndExpand 不起作用?

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

【问题讨论】:

  • 如果父视图大于其所有子视图的组合大小,即额外空间可用,则空间在具有该后缀的子视图之间按比例分配。那些孩子会“占据”他们的空间,但不一定会“填满”它。stackoverflow.com/questions/25338533/…
  • @AndroDevil 哇,这出乎意料。在我在这里发布之前,我实际上阅读了这个问题,但这对我来说没有意义。因为如果空间给了一个元素,并且元素默认布局选项是填充,它应该填充那个可用空间。但是不,StackLayout 更像是 FlexLayout 之间的空间。它只是在元素之间分配空间而不给它们。太棒了。

标签: android xamarin.forms


【解决方案1】:

参考@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>

我得到了什么:

所以Expand 的意思如下:

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

【讨论】:

    【解决方案2】:

    原因: 在 stackLayout 中添加标签时,stackLayout 不会使子视图适合大小。

    解决方案:

    将标签放在网格中。参考以下代码。

    例如:如果要将标签“Hi”设置为屏幕宽度的一半

    <Grid BackgroundColor="Blue" >
        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>
    
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
    
        <Label  HeightRequest="100" Grid.Row="0" Grid.Column="0"  Text="Hi" BackgroundColor="Red" />
        <BoxView  Grid.Row="0" Grid.Column="1" BackgroundColor="Red"
                     HeightRequest="30"
                     HorizontalOptions="End"
                     VerticalOptions="Center"
                     WidthRequest="30"/>
    
    </Grid>
    

    【讨论】:

    • 感谢卢卡斯的回答,但似乎不仅关注标签,还关注其他观点。 Expand 要求提供更多空间,但不会使用它,除非它也有 Fill,否则它只会在使用 StartCenterEnd 时将自己锚定在可用空间内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多