xaml - ScrollView 中的 Xamarin Center StackLayout

标签 xaml layout xamarin xamarin.forms

我正在尝试使 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 上的 Horizo​​ntalOptions 似乎没有任何影响,但我会使用 FillAndExpand 作为一种方式来记录填充 ScrollView 的整个水平空间的意图。

关于xaml - ScrollView 中的 Xamarin Center StackLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752664/

相关文章:

java - 绑定(bind) BorderPane 和框架高度和宽度

android - View 中的位图未在 Android 上显示

xamarin - 如何将可观察集合绑定(bind)到 xamarin 中的 flexlayout?

c# - Xamarin C# 一个 UITableView 中的两个 UITableViewCells

html - Bootstrap V2 "boxed"(1170px) 布局中的全宽面板

xamarin - 为什么当我单击按钮时事件指示器需要近 3 秒才能启动

c# - 当不将子节点添加到vm的构造函数中时,WPF TreeView 不显示子节点

c# - wpf 形式的所有内容的圆角

c# - 如何在作为 List<string> 的 ObservableCollection 绑定(bind)属性(到 ListView)上调用 String.Join

c# - ControlTemplate.Triggers 中更灵活的 setter