1
不同布局的预览如下:
StackLayout:一维堆栈布局,只能进行横向或纵向布局,允许嵌套。可以理解为行或列为1的Grid
从左到右依次为纵向,横向,嵌套
嵌套部分代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="StackLayoutDemos.Views.CombinedStackLayoutPage"
Title="Combined StackLayouts demo">
<StackLayout Margin="20">
...
<Frame BorderColor="Black"
Padding="5">
<StackLayout Orientation="Horizontal"
Spacing="15">
<BoxView Color="Red" />
<Label Text="Red"
FontSize="Large"
VerticalOptions="Center" />
</StackLayout>
</Frame>
<Frame BorderColor="Black"
Padding="5">
<StackLayout Orientation="Horizontal"
Spacing="15">
<BoxView Color="Yellow" />
<Label Text="Yellow"
FontSize="Large"
VerticalOptions="Center" />
</StackLayout>
</Frame>
<Frame BorderColor="Black"
Padding="5">
<StackLayout Orientation="Horizontal"
Spacing="15">
<BoxView Color="Blue" />
<Label Text="Blue"
FontSize="Large"
VerticalOptions="Center" />
</StackLayout>
</Frame>
...
</StackLayout>
</ContentPage>
AbsoluteLayout:绝对布局,以左上角为基点的布局,也支持按比例布局。对于Position只要把AbsoluteLayout.LayoutFlags设置为PositionProportional属性,那么Position(x,y)就是比例值,其他所有值视为绝对值,对于X,Y,Width,Height,Position,Size等属性也一样。
最大的特点在于能够定位子类。但是官方文档有个警告
使用绝对了绝对值坐标的话,不同设备的中心坐标可能会偏移,所以需要注意一下。
子级调整和比例布局示例如下:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinApp1.MainPage">
<AbsoluteLayout Margin="20">
<BoxView Color="Silver"
AbsoluteLayout.LayoutBounds="0, 10, 200, 5" />
<BoxView Color="Silver"