【问题标题】:Xamarin.Forms: how to position a Circle Image above and between 2 ScrollLayoutXamarin.Forms:如何在 2 ScrollLayout 上方和之间放置圆形图像
【发布时间】:2021-02-24 20:21:10
【问题描述】:

我在一个 Xamarin.Forms 应用 上工作,其中包含一个 HomePage,基于:

  • Image屏幕顶部背景
  • ScrollView 中显示的项目列表

如果列表包含许多信息,ScrollView 可以恢复Image

看起来像这样:

XAML 如下所示:

<Grid RowSpacing="0"
        BackgroundColor="{StaticResource Gray-050}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="1">
        <!--  Header view  -->
        <ScrollView>
            <ContentView x:Name="headerView"
                         HorizontalOptions="FillAndExpand"
                         VerticalOptions="FillAndExpand">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <!-- the BoxView will be replaced by an Image -->
                    <BoxView x:Name="headerImage"
                                HeightRequest="280"
                                BackgroundColor="Yellow" />
                </Grid>
            </ContentView>
        </ScrollView>

        <!--  List view  -->
        <ScrollView HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand">
            <Grid ColumnSpacing="0"
                  RowSpacing="0"
                  VerticalOptions="FillAndExpand">
                <Grid.RowDefinitions>
                    <RowDefinition Height="140" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <!-- Main container -->
                    <yummy:PancakeView CornerRadius="16,16,0,0"
                                       Padding="0,10,0,0"
                                       BackgroundColor="{StaticResource Gray-050}"
                                       Grid.Row="1">
                        <StackLayout BackgroundColor="Transparent"
                                     Spacing="16" Margin="16">

                            <!-- Phone container -->
                            <yummy:PancakeView Style="{StaticResource YummyHomeFrame}"
                                                Padding="16">
                                <Grid RowSpacing="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <Label Style="{StaticResource HomePageHeaderLabelStyle}"
                                            Text="Phone Number" />
                                    <Label Grid.Row="1"
                                            Style="{StaticResource HomePageLabelStyle}"
                                            Text="+33 6 20 10 70 40"  />
                                </Grid>
                            </yummy:PancakeView>

                            <!-- Other containers -->

                        </StackLayout>
                    </yummy:PancakeView>
                </Grid>
            </Grid>
        </ScrollView>
    </Grid>
</Grid>

我想在 2 个ScrollViews 之间、MainContainer 上方显示一个圆形徽标,如下所示:

但我没有设法做到这一点,我不知道它是否可能......

【问题讨论】:

  • 您是否尝试将圆形徽标放在 2 个滚动视图之间并与您的标题一起滚动?如果是这样,我最初的想法是&lt;ScrollView&gt;&lt;Grid&gt;&lt;Header ContentView /&gt; &lt;ListView ScrollOrGrid/&gt; &lt;CircleLogo HorizontalOptions="Center" VerticalOptions="Start" Margin="0,100,0,0" /&gt;&lt;/Grid&gt; &lt;/ScrollView&gt;。您应该调整页边距以使其适合页面,只需关注同一级别的标题和列表视图。
  • 您好,您的意思是要继续显示圆圈徽标吗?您可以尝试使用 RelativeLayout 来做。 docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
  • 谢谢@Shaw。是的,我想在 2 ScrollViews 之间放置圆形徽标,并使其与列表 ScrollView 一起滚动,正如我们在 GIF 上看到的那样。您建议我添加另一个 ScrollView 作为主容器,并在主 Grid 中添加 Circle 项?我试过这个,我玩过Margins,但是Circle仍然显示在标题中,而不是2Layouts(标题/列表)上方。
  • @JuniorJiang-MSFT 我试过玩RelativeLayout,但圆圈是静态的:我需要圆圈与ListView一起滚动,正如我们在附加的GIF中看到的那样在描述中。
  • @Gold.strike 您可以将内部控件设置为相对布局来尝试,看看我的答案。

标签: xaml user-interface xamarin.forms scrollview frame


【解决方案1】:

您可以尝试使用RelativeLayout 来实现。

下面是类似的示例代码:

<ScrollView HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand">
        <Grid ColumnSpacing="0"
                RowSpacing="0"
                VerticalOptions="FillAndExpand">
            <Grid.RowDefinitions>
                <RowDefinition Height="140" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <!-- Main container -->
                <RelativeLayout>
                    <Frame CornerRadius="16" x:Name="MainFrame"
                                    Padding="0,10,0,0"
                                    BackgroundColor="LightBlue"
                                    Grid.Row="1">
                    <StackLayout BackgroundColor="Transparent"
                                    Spacing="16"
                                    Margin="16">

                        <!-- Phone container -->
                        <Frame Padding="16">
                            <Grid RowSpacing="0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Label Text="Phone Number" />
                                <Label Grid.Row="1"
                                        Text="+33 6 20 10 70 40" />
                            </Grid>
                        </Frame>

                        <!-- Other containers -->

                    </StackLayout>
                </Frame>
                    <BoxView CornerRadius="25"
                                BackgroundColor="AliceBlue"
                                RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=MainFrame, Property=Width,Factor=0.45}"
                                RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=MainFrame, Property=Y,Constant=-20}"
                                RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToView, ElementName=MainFrame,  Constant=100}"
                                RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToView, ElementName=MainFrame, Constant=100}" />
                </RelativeLayout>
            </Grid>
        </Grid>
</ScrollView>

效果:

【讨论】:

  • 在这种情况下,RelativeLayout 是静态的,不能滚动,这不是预期的行为。
  • @Gold.strike 因为无法重现所有需要的效果,您可以在这里分享一个示例。我会在本地网站上查看。
  • 您可以在此处获取完整的 XAML 源代码(不带圆圈徽标):gofile.io/d/ml59CS
  • @Gold.strike 感谢分享。但是,我无法下载,因为该链接将重定向到另一个无法下载的链接。如果可能的话,你可以把代码分享到 Github 上,免费又方便。
【解决方案2】:

在容器的同一层添加你的圆圈标志。

                <!--  Items view  -->
                <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                    <Grid
                        ColumnSpacing="0"
                        RowSpacing="0"
                        VerticalOptions="FillAndExpand">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="140" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="1">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <!--  Container  -->
                            <Frame ...>

                            <!--  Yoour logo -->
                            <Frame
                                Grid.Row="1"
                                Margin="0,-40,0,0"
                                BackgroundColor="Green"
                                CornerRadius="40"
                                HeightRequest="40"
                                HorizontalOptions="Center"
                                VerticalOptions="Start"
                                WidthRequest="40" />
                        </Grid>
                    </Grid>
                </ScrollView>

查看演示:

由于我没有使用 nugget 包,我只是用框架替换了 pancake 视图。并且您可以根据需要调整徽标大小、边距。

顺便说一句,你的结构有点复杂或冗余,可能是一个 4 级的主要结构,如下所示:

<Grid x:Name="mainGrid">

  <!--  Header view  -->
  <BoxView (or Image).../>

  <!--  Items view  -->
  <ScrollView ...>
    <Grid ...>

      <!--  Container  -->
      <Frame ...>

      <!--  logo  -->
      <Frame (or Image)...>

    </Grid>
  </ScrollView>
</Grid>

【讨论】:

    【解决方案3】:

    谢谢@Shaw 和@Junior Jiang - MSFT

    2 种解决方案withwithout RelativeLayout

    没有RelativeLayout的版本由@Shaw提供。

    <Grid RowSpacing="0"
            BackgroundColor="LightGray"
            x:Name="mainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="1">
            <!--  Header view  -->
            <ScrollView>
                <ContentView x:Name="headerView"
                                HorizontalOptions="FillAndExpand"
                                VerticalOptions="FillAndExpand">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <!-- the BoxView can be replaced by an Image -->
                        <BoxView x:Name="headerImage"
                                    HeightRequest="280"
                                    BackgroundColor="Yellow" />
                    </Grid>
                </ContentView>
            </ScrollView>
                <!-- Content View -->
            <ScrollView HorizontalOptions="FillAndExpand"
                        VerticalOptions="FillAndExpand">
                <Grid ColumnSpacing="0"
                        RowSpacing="0"
                        VerticalOptions="FillAndExpand">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="140" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="1">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <!-- Frame Container-->
                        <Frame x:Name="MainFrame"
                                CornerRadius="16"
                                Padding="0,10,0,0"
                                BackgroundColor="LightGray"
                                Grid.Row="1">
                            <StackLayout BackgroundColor="Transparent"
                                            Spacing="16" Margin="16,48,16,16">
                                <!-- Phone Frame -->
                                <Frame Padding="16">
                                    <Grid RowSpacing="0">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="*" />
                                        </Grid.RowDefinitions>
                                        <Label Text="Phone Number" />
                                        <Label Grid.Row="1"
                                                Text="+33 6 20 10 70 40"  />
                                    </Grid>
                                </Frame>
                                <!-- Other Frames -->
                                <!-- ... -->
                            </StackLayout>
                        </Frame>
                        <!-- No RelativeLayout positioned item -->
                        <BoxView Grid.Row="1"
                                    CornerRadius="45"
                                    BackgroundColor="DarkBlue"
                                    WidthRequest="90" HeightRequest="90"
                                    VerticalOptions="Start" HorizontalOptions="Center"
                                    Margin="0,-45,0,0" />
                    </Grid>
                </Grid>
            </ScrollView>
        </Grid>
    </Grid>
    

    带有 RelativeLayout 的版本基于@Junior Jiang - MSFT 的建议:

    <Grid RowSpacing="0"
            BackgroundColor="LightGray"
            x:Name="mainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="1">
            <!--  Header View  -->
            <ScrollView>
                <ContentView x:Name="headerView"
                                HorizontalOptions="FillAndExpand"
                                VerticalOptions="FillAndExpand">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <!-- the BoxView can be replaced by an Image -->
                        <BoxView x:Name="headerImage"
                                    HeightRequest="280"
                                    BackgroundColor="Yellow" />
                    </Grid>
                </ContentView>
            </ScrollView>
            <!-- Content View -->
            <ScrollView HorizontalOptions="FillAndExpand"
                        VerticalOptions="FillAndExpand">
                <Grid ColumnSpacing="0"
                        RowSpacing="0"
                        VerticalOptions="FillAndExpand">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="140" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <StackLayout Grid.Row="1">
                        <RelativeLayout>
                            <!-- Frame container -->
                            <Frame x:Name="MainFrame"
                                    CornerRadius="16"
                                    Padding="0,10,0,0"
                                    BackgroundColor="LightGray"
                                            Grid.Row="1">
                                <StackLayout BackgroundColor="Transparent"
                                            Spacing="16" Margin="16,48,16,16">
                                    <!-- Phone Frame -->
                                    <Frame Padding="16">
                                        <Grid RowSpacing="0">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*" />
                                                <RowDefinition Height="*" />
                                            </Grid.RowDefinitions>
                                            <Label Text="Phone Number" />
                                            <Label Grid.Row="1"
                                                Text="+33 6 20 10 70 40"  />
                                        </Grid>
                                    </Frame>
                                    <!-- Other Frames -->
                                    <!-- ... -->
                                </StackLayout>
                            </Frame>
                            <!-- RelativeLayout positioned item -->
                            <BoxView CornerRadius="45"
                                    BackgroundColor="DarkBlue"
                                    WidthRequest="90" HeightRequest="90"
                                    VerticalOptions="Center" HorizontalOptions="Center"
                                    RelativeLayout.XConstraint="{ConstraintExpression 
                                                                    Type=RelativeToParent,
                                                                    Property=Width, 
                                                                    Factor=0.5,
                                                                    Constant=-45}"
                                    RelativeLayout.YConstraint="{ConstraintExpression
                                                                    Type=RelativeToView,
                                                                    ElementName=MainFrame,
                                                                    Property=Y,Constant=-45}" />
                        </RelativeLayout>
                    </StackLayout>
                </Grid>
            </ScrollView>
        </Grid>
    </Grid>
    

    【讨论】:

    • 太棒了!如果已经解决了,记得在有时间的时候标记你的答案。它将帮助遇到同样问题的其他人。
    猜你喜欢
    • 2017-04-28
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2018-07-28
    • 2021-03-21
    • 1970-01-01
    相关资源
    最近更新 更多