【问题标题】:Advanced positioning inside a scrollview in Xamarin FormsXamarin Forms 中滚动视图内的高级定位
【发布时间】:2016-12-19 15:11:59
【问题描述】:

我目前正在使用滚动视图并使用网格和堆栈布局来定位事物。当我做非常基本的布局时它工作正常,例如我的视图首先是一个图像,然后是很多文本,但是如果你继续滚动一点,我希望进一步向下创建一个非常高级的布局,目标是它看起来像这样:

我不太清楚如何解决这个问题并在不使用 absolutelayout 的情况下在滚动视图中完成这样的布局(我曾尝试在滚动视图中使用它,但它一直给我带来麻烦,事情没有出现等)。

这是我的带有图像和文本的基本代码:

<ScrollView>
 <Grid>
 <Grid.RowDefinitions>
 <RowDefinition Height="*"></RowDefinition>
 <RowDefinition Height="Auto"></RowDefinition>

  </Grid.RowDefinitions>
  <Image Aspect = "AspectFit" Grid.Row="0" x:Name = "image" />

        <StackLayout Grid.Row="1" Padding = "10,10,10,0" >

        <Label XAlign = "Center" x:Name = "name" FontSize = "15" FontAttributes = "Bold" TextColor = "Black" FontFamily = "Helvetica Neue" />

        <Label XAlign = "Center" x:Name = "date" FontSize = "13" TextColor = "Black" FontFamily = "Helvetica Neue"  />

        <StackLayout Padding = "20,20,20,0" >

        <Label x:Name = "text" FontSize = "12" TextColor = "Black" FontFamily = "Helvetica Neue" />
        </StackLayout> 
        </StackLayout>

  </Grid>
</ScrollView>

我需要如何继续我的代码才能为我在上面发布的图片添加类似的布局?

【问题讨论】:

    标签: c# xamarin xamarin.forms scrollview


    【解决方案1】:

    只需添加一个 StackLayout 作为 ScrollView 的第一个子项,然后在其中添加另一个 Grid 在第一个子项下方。或者在 Grid.Row=1 中的 StackLayout 中

    <ScrollView>
      <StackLayout>
          <Grid>
              <Grid.RowDefinitions>
                  <RowDefinition Height="*"></RowDefinition>
                  <RowDefinition Height="Auto"></RowDefinition>
              </Grid.RowDefinitions>
              <Image Aspect = "AspectFit" Grid.Row="0" x:Name = "image" />
    
              <StackLayout Grid.Row="1" Padding = "10,10,10,0" >
    
                  <Label XAlign = "Center" x:Name = "name" FontSize = "15" FontAttributes = "Bold" TextColor = "Black" FontFamily = "Helvetica Neue" />
    
                  <Label XAlign = "Center" x:Name = "date" FontSize = "13" TextColor = "Black" FontFamily = "Helvetica Neue"  />
    
                  <StackLayout Padding = "20,20,20,0" >
    
                      <Label x:Name = "text" FontSize = "12" TextColor = "Black" FontFamily = "Helvetica Neue" />
                  </StackLayout> 
              </StackLayout>
           </Grid>
           <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
    
                Your content here
    
           </Grid>
        </StackLayout>
    </ScrollView>
    

    【讨论】:

    • 唯一的问题是图像` `没有到达屏幕边缘,因为滚动视图下的第一个堆栈布局。知道如何解决这个问题吗?
    • 尝试在所有父布局上添加 Horizo​​ntalOptions="FillAndExpand"
    • 将其添加到滚动视图、堆栈布局、网格以及图像本身,但图像不会将自身定位到边缘。当我尝试其他图片时,有时图像会变得更小,所以它就像中心的一个小立方体。我需要将第一个``更改为自动吗?
    • 抱歉,之前没有看到“AspectFit”,试试 AspectFill 或者只是在图像上填充。 AspectFit 是您最好的选择,因为它可以保持图像源的纵横比/比例
    猜你喜欢
    • 1970-01-01
    • 2021-08-20
    • 2017-11-29
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多