目录
可以在应用程序中排列和分组 UI 控件。 选择布局类需要了解布局如何定位其子元素,以及布局如何调整其子元素的大小。 此外,可能需要嵌套布局来创建所需的布局。
我们介绍5个布局控件使用
Grid
HorizontalStackLayout
VerticalStacklayout
AbsoluteLayout
FlexLavout
1 Grid网格
RowSpacing ColumnSpacing 行间距 列间距
Grid.RowDefinitions 定义行
Grid.ColDefinitions 定义列
<Grid RowSpacing="5" ColumnSpacing="5">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="1"/>
<BoxView Color="Aqua" Grid.Row="1" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="1" Grid.Column="1"/>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="1"/>
</Grid>
按比例分配网格
定义3行2列 第一行占1份,第二行占2份 第三行占3份
<Grid RowSpacing="5" ColumnSpacing="5">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="1"/>
<BoxView Color="Aqua" Grid.Row="1" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="1" Grid.Column="1"/>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="1"/>
</Grid>
Grid简写
定义3行2列简写
<Grid RowDefinitions="1*,2*,3*" ColumnDefinitions="*,*" RowSpacing="5" ColumnSpacing="5">
定义4行 3列简写
<Grid RowDefinitions="*,*,*,*" ColumnDefinitions="*,*,*" RowSpacing="5" ColumnSpacing="5">
<Grid RowDefinitions="1*,2*,3*" ColumnDefinitions="*,*" RowSpacing="5" ColumnSpacing="5">
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="1"/>
<BoxView Color="Aqua" Grid.Row="1" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="1" Grid.Column="1"/>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="1"/>
</Grid>
Grid合并
Grid合并列 Grid.ColumnSpan="3"(合并3列)Grid.RowSpan="3"(合并3行)
Grid 嵌套
第2行2列合并
嵌套 1行3列
<Grid RowDefinitions="*,*,*" ColumnDefinitions="*,*" RowSpacing="5" ColumnSpacing="5">
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="0" Grid.Column="1"/>
<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" ColumnDefinitions="*,*,*" ColumnSpacing="5">
<BoxView Color="Aqua" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Column="1"/>
<BoxView Color="Aqua" Grid.Column="2"/>
</Grid>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="0"/>
<BoxView Color="Aqua" Grid.Row="2" Grid.Column="1"/>
</Grid>
2 HorizontalStackLayout水平堆叠布局
子控件水平堆叠
Spacing="10" 子控件间距10像素
HeightRequest="200" HorizontalStackLayout高度为200像素
<HorizontalStackLayout Spacing="10" HeightRequest="200">
<Label Text="一" Background="red"/>
<Label Text="二" Background="orange"/>
<Label Text="三" Background="blue"/>
<Label Text="四" Background="green"/>
</HorizontalStackLayout>
对齐子控件
VerticalOptions 默认Center 居中
<HorizontalStackLayout Spacing="10" HeightRequest="200" VerticalOptions="Start">
子控件在HorizontalStackLayout顶部左侧对齐
<HorizontalStackLayout Spacing="10" HeightRequest="200" VerticalOptions="Center">
<HorizontalStackLayout Spacing="10" HeightRequest="200" VerticalOptions="End">
<HorizontalStackLayout Spacing="10" VerticalOptions="Fill">
子控件VerticalOptions
<HorizontalStackLayout Spacing="10" HeightRequest="200">
<Label Text="一" Background="red" VerticalOptions="Start"/>
<Label Text="二" Background="orange" VerticalOptions="Center"/>
<Label Text="三" Background="blue" VerticalOptions="End"/>
<Label Text="四" Background="green" VerticalOptions="Fill"/>
</HorizontalStackLayout>
实例应用
在HorizontalStackLayout布局动态添加BoxView控件并横向滚动
<Grid RowDefinitions="*,70" Padding="5" BackgroundColor="#f5f3fe">
<ScrollView Orientation="Horizontal" BackgroundColor="White">
<HorizontalStackLayout x:Name="hStack" Spacing="10"
Margin="10" HeightRequest="200"/>
</ScrollView>
<Grid Grid.Row="1" ColumnDefinitions="60,*,100" ColumnSpacing="10" Padding="10">
<Label Text="宽度" VerticalOptions="Center" HorizontalOptions="End"/>
<Entry x:Name="entryWidth" Grid.Column="1" WidthRequest="150"
HorizontalOptions="Start"
Placeholder="请输入项目宽度"
ClearButtonVisibility="WhileEditing"/>
<Button x:Name="btnAdd" Text="添加项目" Grid.Column="2" Clicked="btnAdd_Clicked"/>
</Grid>
private void btnAdd_Clicked(object sender, EventArgs e)
{
hStack.Add(new BoxView
{
Color = Color.FromRgb(0x70, 0x7c, 0xd2),
WidthRequest = double.Parse(entryWidth.Text),
CornerRadius = 10
});
}
3 VerticalStacklayout垂直堆叠布局
<VerticalStackLayout Margin="20" Spacing="6">
<Label Text="Start" BackgroundColor="gray"/>
<Label Text="Center" BackgroundColor="gray"/>
<Label Text="End" BackgroundColor="gray"/>
<Label Text="Fill" BackgroundColor="gray"/>
</VerticalStackLayout>
<VerticalStackLayout Margin="20" Spacing="6" >
<Label Text="Start" BackgroundColor="gray" HorizontalOptions="Start"/>
<Label Text="Center" BackgroundColor="gray" HorizontalOptions="Center"/>
<Label Text="End" BackgroundColor="gray" HorizontalOptions="End"/>
<Label Text="Fill" BackgroundColor="gray"HorizontalOptions="Fill" />
</VerticalStackLayout>
实例应用
利用VerticalStacklayout水平堆叠布局 三原色 次生色表达
<VerticalStackLayout Margin="20" Spacing="6" >
<Label Text="Primay Colors" BackgroundColor="silver" />
<Frame BorderColor="Silver" Padding="5">
<HorizontalStackLayout Spacing="15">
<Rectangle Fill="red" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>
<Label Text="Red" VerticalOptions="Center"/>
</HorizontalStackLayout>
</Frame>
<Frame BorderColor="Silver" Padding="5">
<HorizontalStackLayout Spacing="15">
<Rectangle Fill="Yellow" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>
<Label Text="Yellow" VerticalOptions="Center"/>
</HorizontalStackLayout>
</Frame>
<Frame BorderColor="Silver" Padding="5">
<HorizontalStackLayout Spacing="15">
<Rectangle Fill="Blue" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>
<Label Text="Blue" VerticalOptions="Center"/>
</HorizontalStackLayout>
</Frame>
<Label Text="Secondary Colors" BackgroundColor="silver" />
<Frame BorderColor="Silver" Padding="5">
<HorizontalStackLayout Spacing="15">
<Rectangle Fill="Green" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>
<Label Text="Green" VerticalOptions="Center"/>
</HorizontalStackLayout>
</Frame>
<Frame BorderColor="Silver" Padding="5">
<HorizontalStackLayout Spacing="15">
<Rectangle Fill="Orange" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>
<Label Text="Orange" VerticalOptions="Center"/>
</HorizontalStackLayout>
</Frame>
<Frame BorderColor="Silver" Padding="5">
<HorizontalStackLayout Spacing="15">
<Rectangle Fill="Purple" HeightRequest="30" WidthRequest="30" HorizontalOptions="Start"/>
<Label Text="Purple" VerticalOptions="Center"/>
</HorizontalStackLayout>
</Frame>
</VerticalStackLayout>
4 AbsoluteLayout 绝对定位布局
输入绝对值
<AbsoluteLayout Margin="20">
<BoxView Color="Silver" AbsoluteLayout.LayoutBounds="0,10,200,5"/>
<BoxView Color="Silver" AbsoluteLayout.LayoutBounds="0,20,200,5"/>
<BoxView Color="Silver" AbsoluteLayout.LayoutBounds="10,10,5,65"/>
<BoxView Color="Silver" AbsoluteLayout.LayoutBounds="20,0,5,65"/>
<Label Text="Stylish Header" FontSize="24" TextColor="Gray"
AbsoluteLayout.LayoutBounds="30,25"/>
</AbsoluteLayout>
按比例
<BoxView Color="Blue" AbsoluteLayout.LayoutBounds="0.5,0,100,25" AbsoluteLayout.LayoutFlags="all" />
0.5,0,100,25 按比例
<AbsoluteLayout>
<BoxView Color="Blue" AbsoluteLayout.LayoutBounds="0.5,0,100,25"
AbsoluteLayout.LayoutFlags="PositionProportional"
/>
<BoxView Color="Green" AbsoluteLayout.LayoutBounds="0,0,25,100"
AbsoluteLayout.LayoutFlags="PositionProportional"
/>
<BoxView Color="Red" AbsoluteLayout.LayoutBounds="1,0,25,100"
AbsoluteLayout.LayoutFlags="PositionProportional"
/>
<BoxView Color="Black" AbsoluteLayout.LayoutBounds="0.5,1,100,25"
AbsoluteLayout.LayoutFlags="PositionProportional"
/>
</AbsoluteLayout>
<AbsoluteLayout>
<BoxView Color="Blue"
AbsoluteLayout.LayoutBounds="0.5,0,0.3,0.1"
AbsoluteLayout.LayoutFlags="All" />
<BoxView Color="Green" AbsoluteLayout.LayoutBounds="0,0.5,0.1,0.3"
AbsoluteLayout.LayoutFlags="All" />
<BoxView Color="Red" AbsoluteLayout.LayoutBounds="1,0.5,0.1,0.3"
AbsoluteLayout.LayoutFlags="All" />
<BoxView Color="Black" AbsoluteLayout.LayoutBounds="0.5,1,0.3,0.1"
AbsoluteLayout.LayoutFlags="All"/>
<Label Text="Centered" FontSize="26"
AbsoluteLayout.LayoutBounds="0.5,0.5"
AbsoluteLayout.LayoutFlags="PositionProportional"/>
</AbsoluteLayout>
5 FlexLavout弹性布局
<FlexLayout JustifyContent="Start">
<Label Text="oneLable" BackgroundColor="Red"/>
<Label Text="towlable" BackgroundColor="green"/>
<Label Text="thirdlable" BackgroundColor="Blue"/>
</FlexLayout>
<FlexLayout JustifyContent="Center">
<FlexLayout JustifyContent="End">
<FlexLayout JustifyContent="SpaceAround">
<FlexLayout JustifyContent="SpaceBetween">
<FlexLayout JustifyContent="SpaceEvenly">
<FlexLayout Direction="Column" JustifyContent="SpaceEvenly">
<FlexLayout Direction="ColumnReverse" JustifyContent="SpaceEvenly">
<FlexLayout Direction="RowReverse" JustifyContent="SpaceEvenly">
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="Center">
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="end">
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="Start">
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="Stretch">
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="Stretch" Wrap="Wrap">
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="Stretch" Wrap="Wrap">
<Label Text="1oneLable" BackgroundColor="Red"/>
<Label Text="2towlable" BackgroundColor="green"/>
<Label Text="3thirdlable" BackgroundColor="Blue"/>
<Label Text="4oneLable" BackgroundColor="Red"/>
<Label Text="5towlable" BackgroundColor="green"/>
<Label Text="6thirdlable" BackgroundColor="Blue"/>
<Label Text="7oneLable" BackgroundColor="Red"/>
</FlexLayout>
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="Stretch" Wrap="NoWrap">
<FlexLayout JustifyContent="SpaceEvenly" AlignItems="Stretch" Wrap="Reverse">
<FlexLayout JustifyContent="SpaceEvenly" Wrap="Wrap" >
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
<BoxView BackgroundColor="Silver" WidthRequest="100" HeightRequest="100"/>
</FlexLayout>
<FlexLayout JustifyContent="SpaceEvenly" Wrap="Wrap" AlignContent="SpaceEvenly" >
“圣杯布局”
<FlexLayout Direction="Column" >
<Label Text="页眉" FontSize="18" BackgroundColor="Aqua" HorizontalTextAlignment="Center"/>
<FlexLayout FlexLayout.Grow="1">
<Label Text="内容" FontSize="18" BackgroundColor="Gray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FlexLayout.Grow="1"/>
<BoxView Color="blue" FlexLayout.Basis="50" FlexLayout.Order="-1"/>
<BoxView Color="Green" FlexLayout.Basis="50"/>
</FlexLayout>
<Label Text="页脚" FontSize="18" BackgroundColor="Pink" HorizontalTextAlignment="Center"/>
</FlexLayout>
基本上布局也算完善了,微软MAui介绍不系统,遇到问题,下手查找资料,也解决不了什么问题,网络搜索,要不就是介绍,要不就是点功能描述,也许还不是很流行MAUi 相关教程还是比较少,与小伙伴共同进步。