我正在尝试使 ScrollView
中的 StackLayout
水平居中。但是,StackLayout
不是水平居中的(它是左对齐的)。我尝试将 ScrollView
居中,但整个 View 不可滚动 - 只有中心部分可以滚动。
<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
。有什么想法吗?
最佳答案
您有几个选项,下面示例中的每个标签都应居中显示。
关键是 StackLayout 的布局基于其内容。您可能认为您可以像以前那样将整个 StackLayout 居中,但至少作为 ScrollView 的内容,它不会那样工作。但是将 StackLayout 的子项居中将在 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 上的 HorizontalOptions 似乎没有任何影响,但我会使用 FillAndExpand 作为一种方式来记录填充 ScrollView 的整个水平空间的意图。
关于xaml - ScrollView 中的 Xamarin Center StackLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752664/