关于AbsoluteLayout,官方文档有说明:
AbsoluteLayout 有一个独特的锚点模型,当使用比例定位时,元素的锚点相对于其元素定位,因为元素相对于布局定位。使用绝对定位时,锚点位于视图内的 (0,0) 处。这有两个重要的后果:
- 不能使用比例值将元素定位在屏幕外。
- 无论布局或设备的大小如何,元素都可以可靠地沿布局的任何一侧或中心定位。
AbsoluteLayout 与RelativeLayout 一样,能够定位元素以使它们重叠。
注意在共享链接中,框的锚点是一个白点。请注意锚点和框在布局中移动时的关系。
也许这似乎很难理解,但是AbsoluteLayout 是这样的。这里是一个关于Anchor 在AbsoluteLayout 中如何工作的示例代码。
<AbsoluteLayout HeightRequest="200" BackgroundColor="Yellow">
<Label BackgroundColor="YellowGreen" Text = "labeone1" AbsoluteLayout.LayoutBounds="0, 0, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="Red" Text = "labetwo2" AbsoluteLayout.LayoutBounds="0.1, 0.1, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="Blue" Text = "labethree3" AbsoluteLayout.LayoutBounds="0.2, 0.2, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="White" Text = "labefour4" AbsoluteLayout.LayoutBounds="0.3, 0.3, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="Gray" Text = "labefive5" AbsoluteLayout.LayoutBounds="0.4, 0.4, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="Green" Text = "labesix6" AbsoluteLayout.LayoutBounds="0.5, 0.5, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="BlueViolet" Text = "labeseven7" AbsoluteLayout.LayoutBounds="0.6, 0.6, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="AliceBlue" Text = "labeeight8" AbsoluteLayout.LayoutBounds="0.7, 0.7, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="BlueViolet" Text = "labenine9" AbsoluteLayout.LayoutBounds="0.8, 0.8, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="DarkSlateGray" Text = "labeten10" AbsoluteLayout.LayoutBounds="0.9,0.9, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="Orange" Text = "labeeleven11" AbsoluteLayout.LayoutBounds="1, 1, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="OrangeRed" Text = "labeeleven12" AbsoluteLayout.LayoutBounds="1.1, 1.1, 0.5, 0.09" AbsoluteLayout.LayoutFlags="All"/>
</AbsoluteLayout>
如果像这样使用AbsoluteLayout,它会起作用:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HeightRequest="50" Padding="10">
<AbsoluteLayout>
<Image Source="{Binding ImageUrl}" BackgroundColor="Aqua" AbsoluteLayout.LayoutBounds="0, 0, 0.5, 1" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="Red" Text = "{Binding Name}" AbsoluteLayout.LayoutBounds="1, 0, 0.5, 0.5" AbsoluteLayout.LayoutFlags="All"/>
<Label BackgroundColor="Blue" Text = "{Binding Location}" AbsoluteLayout.LayoutBounds="1, 1, 0.5, 0.5" AbsoluteLayout.LayoutFlags="All"/>
</AbsoluteLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
这里AbsoluteLayout不是实现你想要的最佳解决方案,你可以尝试使用ViewCell中的Grid布局如下:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10,10,10,10">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"></ColumnDefinition>
<ColumnDefinition Width="50*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Grid.RowSpan="2" Source="{Binding Location}" BackgroundColor="Accent"/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Name}" BackgroundColor="Red" TextColor="White" FontSize="Large" HorizontalOptions="Start" VerticalOptions="Center"></Label>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding Type}" BackgroundColor="Green" TextColor="White" FontSize="Large" HorizontalOptions="Start" VerticalOptions="Center"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>