【问题标题】:Wrapping one of two stacked FlexLayouts包装两个堆叠的 FlexLayout 之一
【发布时间】:2020-08-04 16:46:58
【问题描述】:

我有一个包含两个 FlexLayout 的水平 StackLayout。 StackLayout 在屏幕的右上角对齐。现在我希望左边的 FlexLayout(这里称为 LeftStack,见下面的代码)能够水平增长,并且一旦单行的项目太多;换行。我尝试了两件事

  1. 将 Wrap 属性设置为 Wrap,但左侧的 FlexLayout 然后立即包装每个项目,而不是水平扩展 FlexLayout,我得到一列项目:(

  2. 将方向设置为 RowReverse 会导致左侧 FlexLayout 的所有项目消失。嗯,这是出乎意料的:(

如果单行的项目太多,如何让左侧 FlexLayout 换行?第一个包装的项目应该显示在深绿色的 BoxView 下方。屏幕截图显示了我想要实现的目标(而不是发布代码的结果)。任何想法如何使左侧 FlexLayout 换行?

)

<Grid VerticalOptions="Start">
        <StackLayout
            HorizontalOptions="End"
            Margin="0,50,0,0"
            Orientation="Horizontal"
            BackgroundColor="White">
            
            <FlexLayout x:Name="LeftStack" 
                        Direction="Row"
                        Wrap="Wrap">
                <BoxView BackgroundColor="DarkRed" WidthRequest="100" HeightRequest="60" FlexLayout.Grow="1"/>
                <BoxView BackgroundColor="DarkGray" WidthRequest="100" HeightRequest="60" FlexLayout.Grow="1"/>
            </FlexLayout>

            <FlexLayout x:Name="RightStack"
                        Direction="Row">
                <BoxView BackgroundColor="LightCoral" WidthRequest="60" HeightRequest="60" FlexLayout.Grow="1"/>
                <BoxView BackgroundColor="LightGreen" WidthRequest="60" HeightRequest="60" FlexLayout.Grow="1"/>
        </FlexLayout>
    </StackLayout>
</Grid>

【问题讨论】:

  • 你好,很抱歉没有太了解你想要什么,你能分享另一张图片来解释需要的效果吗?
  • @JuniorJiang-MSFT 我改变了我的问题,以便更清楚我想要实现的目标

标签: xamarin.forms


【解决方案1】:

如果设置了LeftStackWidthRequest,那么就可以横向增长了。并且需要删除每个BoxView中的FlexLayout.Grow

...
<FlexLayout x:Name="LeftStack"
            JustifyContent="End"
            WidthRequest="300"
            Wrap="Wrap">
    <BoxView BackgroundColor="DarkRed"
                WidthRequest="100"
                HeightRequest="60" />
    <BoxView BackgroundColor="DarkGray"
                WidthRequest="100"
                HeightRequest="60"/>
    <BoxView BackgroundColor="DarkGreen"
                WidthRequest="100"
                HeightRequest="60" />
    <BoxView BackgroundColor="DarkKhaki"
                WidthRequest="100"
                HeightRequest="60" />
    <BoxView BackgroundColor="DarkMagenta"
                WidthRequest="100"
                HeightRequest="60" />
    <BoxView BackgroundColor="Firebrick"
                WidthRequest="100"
                HeightRequest="60" />
    <BoxView BackgroundColor="DarkCyan"
                WidthRequest="100"
                HeightRequest="60"/>
    <BoxView BackgroundColor="DarkSlateGray"
                WidthRequest="100"
                HeightRequest="60" />
</FlexLayout>
...

效果:

注意:如果需要设置从右到左的增长方向,需要设置JustifyContent="End"。否则,默认为从左到右。

====================================更新=========== ======================

也可以使用FlexLayout作为根布局,那么就不需要设置宽度了。

<FlexLayout Direction="Row" Wrap="Wrap" JustifyContent="End" BackgroundColor="AntiqueWhite">

    <FlexLayout x:Name="LeftStack"
                FlexLayout.Grow="3"
                JustifyContent="End"
                Wrap="Wrap">
        <BoxView BackgroundColor="DarkRed"
                    WidthRequest="100"
                    HeightRequest="60" />
        <BoxView BackgroundColor="DarkGray"
                    WidthRequest="100"
                    HeightRequest="60"/>
        <BoxView BackgroundColor="DarkGreen"
                    WidthRequest="100"
                    HeightRequest="60" />
        <BoxView BackgroundColor="DarkKhaki"
                    WidthRequest="100"
                    HeightRequest="60" />
        <BoxView BackgroundColor="DarkMagenta"
                    WidthRequest="100"
                    HeightRequest="60" />
        <BoxView BackgroundColor="Firebrick"
                    WidthRequest="100"
                    HeightRequest="60" />
        <BoxView BackgroundColor="DarkCyan"
                    WidthRequest="100"
                    HeightRequest="60"/>
        <BoxView BackgroundColor="DarkSlateGray"
                    WidthRequest="100"
                    HeightRequest="60" />

</FlexLayout>

    <FlexLayout x:Name="RightStack" FlexLayout.Grow="1" Margin="10,0,0,0"
                Direction="Row">
        <BoxView BackgroundColor="LightCoral"
                    WidthRequest="60"
                    HeightRequest="60"
                    FlexLayout.Grow="1" />
        <BoxView BackgroundColor="LightGreen"
                    WidthRequest="60"
                    HeightRequest="60"
                    FlexLayout.Grow="1" />
    </FlexLayout>
</FlexLayout>

现在效果:

【讨论】:

  • 不幸的是,将方向更改为列并不能解决问题。我想要的是红色 boxview 换行,因为单行中没有足够的空间容纳它。包好后应该放在深蓝色盒子下面
  • @MouseOnMars Okey ,如果设置 WidthRequestLeftStack 将实现这一点,我会更新答案。
  • 有没有办法在没有宽度请求的情况下做到这一点?理想情况下,该解决方案适用于任何类型的设备,因此我无法指定硬编码宽度。
  • @MouseOnMars 这里需要设置宽度请求,因为根布局是 StackLayout 。所以 flexlayout 的 元素 不能 自动 增长 .
  • @MouseOnMars 如果使用FlexLayout 作为根布局不需要设置宽度请求,请查看我的更新答案。
猜你喜欢
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
相关资源
最近更新 更多