我正试图在 ScrollView
内水平居中 StackLayout
. 但是, StackLayout
isn 't horizontally centered (it'左对齐) . 我尝试将 ScrollView
居中,但整个视图不可滚动 - 只有中心部分 .
<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
. 有任何想法吗?
2 回答
您有几个选项,下面示例中的每个Label都应显示为居中 .
关键是StackLayout的布局基于其内容 . 您可能认为您可以按照您的方式将整个StackLayout集中在一起,但至少作为ScrollView的内容,它不会那样工作 . 但是,将StackLayout的子项居中,将在ScrollView中居中:
外部StackLayout上的HorizontalOptions似乎在这种情况下没有任何影响,但我会使用FillAndExpand来记录填充ScrollView的整个水平空间的意图 .
StackLayout
的工作方式是填充一个轴,同时为所有子元素分割另一个轴中的可用空间 . 它不会在"filled"轴上自动调整(在这种情况下为水平),因此将该元素放在该轴上的中心不会产生任何结果 .但是,您可以使用网格实现所需的布局,也可以使用其他布局模型 .
使用网格: