AbsoluteLayout
AbsoluteLayout
定位和调整与其自身大小和位置成比例的子元素或绝对值。可以使用比例值或静态值来定位和调整子视图,并且可以混合比例和静态值。
XAML 中 AbsoluteLayout
的定义如下所示:
<AbsoluteLayout>
<Label Text="I'm centered on iPhone 4 but no other device"
AbsoluteLayout.LayoutBounds="115,150,100,100" LineBreakMode="WordWrap" />
<Label Text="I'm bottom center on every device."
AbsoluteLayout.LayoutBounds=".5,1,.5,.1" AbsoluteLayout.LayoutFlags="All"
LineBreakMode="WordWrap" />
<BoxView Color="Olive" AbsoluteLayout.LayoutBounds="1,.5, 25, 100"
AbsoluteLayout.LayoutFlags="PositionProportional" />
<BoxView Color="Red" AbsoluteLayout.LayoutBounds="0,.5,25,100"
AbsoluteLayout.LayoutFlags="PositionProportional" />
<BoxView Color="Blue" AbsoluteLayout.LayoutBounds=".5,0,100,25"
AbsoluteLayout.LayoutFlags="PositionProportional" />
<BoxView Color="Blue" AbsoluteLayout.LayoutBounds=".5,0,1,25"
AbsoluteLayout.LayoutFlags="PositionProportional, WidthProportional" />
</AbsoluteLayout>
代码中的相同布局如下所示:
Title = "Absolute Layout Exploration - Code";
var layout = new AbsoluteLayout();
var centerLabel = new Label {
Text = "I'm centered on iPhone 4 but no other device.",
LineBreakMode = LineBreakMode.WordWrap};
AbsoluteLayout.SetLayoutBounds (centerLabel, new Rectangle (115, 159, 100, 100));
// No need to set layout flags, absolute positioning is the default
var bottomLabel = new Label { Text = "I'm bottom center on every device.", LineBreakMode = LineBreakMode.WordWrap };
AbsoluteLayout.SetLayoutBounds (bottomLabel, new Rectangle (.5, 1, .5, .1));
AbsoluteLayout.SetLayoutFlags (bottomLabel, AbsoluteLayoutFlags.All);
var rightBox = new BoxView{ Color = Color.Olive };
AbsoluteLayout.SetLayoutBounds (rightBox, new Rectangle (1, .5, 25, 100));
AbsoluteLayout.SetLayoutFlags (rightBox, AbsoluteLayoutFlags.PositionProportional);
var leftBox = new BoxView{ Color = Color.Red };
AbsoluteLayout.SetLayoutBounds (leftBox, new Rectangle (0, .5, 25, 100));
AbsoluteLayout.SetLayoutFlags (leftBox, AbsoluteLayoutFlags.PositionProportional);
var topBox = new BoxView{ Color = Color.Blue };
AbsoluteLayout.SetLayoutBounds (topBox, new Rectangle (.5, 0, 100, 25));
AbsoluteLayout.SetLayoutFlags (topBox, AbsoluteLayoutFlags.PositionProportional);
var twoFlagsBox = new BoxView{ Color = Color.Blue };
AbsoluteLayout.SetLayoutBounds (topBox, new Rectangle (.5, 0, 1, 25));
AbsoluteLayout.SetLayoutFlags (topBox, AbsoluteLayoutFlags.PositionProportional | AbsoluteLayout.WidthProportional);
layout.Children.Add (bottomLabel);
layout.Children.Add (centerLabel);
layout.Children.Add (rightBox);
layout.Children.Add (leftBox);
layout.Children.Add (topBox);
Xamarin.Forms 中的 AbsoluteLayout 控件允许你指定屏幕上你希望子元素出现的确切位置,以及它们的大小和形状(边界)。
根据此过程中使用的 AbsoluteLayoutFlags 枚举,有几种不同的方法可以设置子元素的边界。该 AbsoluteLayoutFlags 枚举包含以下值:
- 全部 :所有尺寸都是成比例的。
- HeightProportional :身高成正比的布局。
- 无 :没有解释。
- PositionProportional :结合 XProportional 和 YProportional。
- SizeProportional :结合 WidthProportional 和 HeightProportional。
- WidthProportional :宽度与布局成比例。
- XProportional :X 属性与布局成比例。
- YProportional :Y 属性与布局成比例。
使用 AbsoluteLayout 容器的布局的过程起初可能看起来有点违反直觉,但稍微使用它会变得熟悉。创建子元素后,要将它们设置在容器中的绝对位置,你需要执行三个步骤。你将需要使用 AbsoluteLayout.SetLayoutFlags()
方法设置分配给元素的标志。你还需要使用 AbsoluteLayout.SetLayoutBounds()
给元素赋予边界的方法。最后,你需要将子元素添加到 Children 集合中。由于 Xamarin.Forms 是 Xamarin 和特定于设备的实现之间的抽象层,因此位置值可以独立于设备像素。这是前面提到的布局标志发挥作用的地方。你可以选择 Xamarin.Forms 控件的布局过程如何解释你定义的值。