ScrollView中的Xamarin Center StackLayout [英] Xamarin Center StackLayout within ScrollView

查看:91
本文介绍了ScrollView中的Xamarin Center StackLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将StackLayoutScrollView内水平居中.但是,StackLayout不在水平居中(它保持左对齐).我尝试将ScrollView居中,但是整个视图不可滚动-仅中心部分位于

I'm trying to horizontally center a StackLayout within a ScrollView. However, the StackLayout isn't horizontally centered (it's left aligned). I tried centering the ScrollView but then the whole view is not scrollable - only the center section is.

<ScrollView BackgroundColor="Teal">
  <StackLayout Spacing="5"
               Padding="30"
               WidthRequest="400"
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand"
               BackgroundColor="Transparent">
       <Label Text="Test"/>
       <Label Text="Test"/>
       <Label Text="Test"/>
       <Label Text="Test"/>
  </StackLayout>
</ScrollView>

居中使用2个嵌套的StackLayout元素,但不使用ScrollView.有什么想法吗?

The centering works with 2 nested StackLayout elements, but it doesn't with the ScrollView. Any ideas?

推荐答案

您有几个选择,下面示例中的每个Label应该居中显示.

You have a couple of options, and each Label in the example below should show up centered.

关键是StackLayout的布局基于其内容.您可能以为可以将整个StackLayout居中放置,但是至少作为ScrollView的内容,它不能那样工作.但是将StackLayout的子项居中将在ScrollView中居中:

The key thing is that the StackLayout bases its layout on its contents. You'd think that you could center the whole StackLayout the way you did, but at least as the content of a ScrollView, it doesn't work that way. But centering the children of that StackLayout will center within the ScrollView:

<ScrollView BackgroundColor="Teal">
  <StackLayout Spacing="5"
               Padding="30"
               WidthRequest="400"
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand"
               BackgroundColor="Transparent">
       <Label Text="Test" HorizontalOptions="Center"/>
       <StackLayout HorizontalOptions="Center">
           <Label Text="Test"/>
       </StackLayout>
  </StackLayout>
</ScrollView>

在这种情况下,外部StackLayout上的Horizo​​ntalOptions似乎没有任何影响,但是我将使用FillAndExpand来记录填充ScrollView整个水平空间的意图.

The HorizontalOptions on the outer StackLayout does not seem to have any impact in this situation, but I would use FillAndExpand as a way to document the intent to fill the entire horizontal space of the ScrollView.

这篇关于ScrollView中的Xamarin Center StackLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆