【问题标题】:Xamarin.Forms: can grid cell include several labels?Xamarin.Forms:网格单元格可以包含多个标签吗?
【发布时间】:2019-07-18 05:58:18
【问题描述】:
其中一个网格单元如下所示:
<Label Text="{Binding Address1}" FontSize="12" TextColor="Black" Grid.Row="1" Grid.Column="0"/>
这显示Address1,这是正确的。
但我不想只显示Address1,而是显示Address1、City、St,每个都有不同的FontSize。
这可以在不改变网格中的行数和列数的情况下完成吗?
【问题讨论】:
标签:
xamarin
xamarin.forms
visual-studio-2017
【解决方案1】:
一种方法是在 Grid 行|列中放置一个 StackLayout,然后分别格式化其中的每个元素:
<Grid....
~~~
<StackLayout Grid.Row="1" Grid.Column="0">
<Label Text="{Binding Address1}" FontSize="12" TextColor="Black" />
<StackLayout Orientation="Horizontal" >
<Label Text="{Binding City}" FontSize="10" TextColor="Black" />
<Label Text="{Binding St}" FontSize="10" TextColor="Black" />
</StackLayout>
<StackLayout>
~~~
</Grid>
【解决方案2】:
当然,你会使用:
<StackLayout Grid.Row="1" Grid.Column="0">
<Label Text="l1"/>
<Label Text="l2"/>
<Label Text="l3"/>
</StackLayout>
【解决方案3】:
您可以按照其他人的建议在您的 Grid 中使用其他布局,例如 StackLayout,但您也可以通过为它们设置相同的 Grid.Row 和 Grid.Column 并设置 HorizontalOptions 和 @ 来将多个视图放在一个 Grid 单元格中987654324@的视图在Grid里面你可以选择每个视图的位置。
例如:
<Grid....
~~~
<Label Grid.Row="0" Grid.Column="1" HorizontalOptions="Start" VerticalOptions="Start" Text="{Binding Address1}" FontSize="12" TextColor="Black" />
<Label Grid.Row="0" Grid.Column="1" HorizontalOptions="Start" VerticalOptions="End" Text="{Binding City}" FontSize="10" TextColor="Black" />
<Label Grid.Row="0" Grid.Column="1" HorizontalOptions="End" VerticalOptions="End" Text="{Binding St}" FontSize="10" TextColor="Black" />
~~~
</Grid>