在Xamarin.Forms中,每个View
都有两个属性HorizontalOptions
和VerticalOptions
。这两种类型都是LayoutOptions
类型,可以具有下列值之一:
LayoutOptions.Start
LayoutOptions.Center
LayoutOptions.End
LayoutOptions.Fill
LayoutOptions.StartAndExpand
LayoutOptions.CenterAndExpand
LayoutOptions.EndAndExpand
LayoutOptions.FillAndExpand
显然,它控制视图在父视图上的对齐方式。但是,每个选项的行为到底是怎样的呢?Fill
和后缀Expand
有什么区别?
发布于 2020-06-23 08:05:56
Falko给了一个很好的解释,但我想用另一个视觉效果来补充,以及这些标记在xaml中是如何工作的,这是我最喜欢使用的。我做了一个简单的项目来测试显示结果。下面是主页的Xaml:
<?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="Alignments.MainPage"
BackgroundColor="White">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="LightGray" Padding="1" Margin="30">
<Label Text="Vert: EndAndExpand, Horz: EndAndExpand" VerticalOptions="EndAndExpand" HorizontalOptions="EndAndExpand" BackgroundColor="White"/>
</StackLayout>
</ContentPage>
正如你所看到的,它是一个非常简单的StackLayout,里面有一个标签。对于下面的每个图像,我保持StackLayout不变,我只是更改了条目的水平和垂直选项,并更改了文本以显示选定的选项,这样您就可以看到条目是如何移动和调整大小的。
以下是用于启动的代码:
<Label Text="Vert: Start, Horz: Start" VerticalOptions="Start" HorizontalOptions="Start" BackgroundColor="White"/>
以及用于StartAndExpand的代码:
<Label Text="Vert: StartAndExpand, Horz: StartAndExpand" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand" BackgroundColor="White"/>
正如您所看到的,除了在StartAndExpand选项中使用了更多的文本之外,在视觉上没有其他区别。这是在我的三星A30物理设备上测试的。这些可能在不同的设备上显示不同,但我认为这里的所有图像都显示了Xamarin中的一些but。对于其余部分,我将只显示屏幕截图,我认为它们是不言而喻的。
我还建议您查看Microsoft documentation以获取更多详细信息。值得注意的是"Expansion仅供StackLayout使用“。
https://stackoverflow.com/questions/25338533
复制相似问题