在相对布局 Xamarin Forms 中将视图与底部和顶部对齐

Align View to bottom and top in RelativeLayout XamarinForms

我想将 Xamarin.Forms 中的 RelativeLayout 的一张图片对齐到顶部,另一张图片对齐到底部。 我该怎么做?

<RelativeLayout>
            <Image 
                Aspect="Fill"
                Source = "header_login.png"></Image>
            <Image 
                Aspect="Fill"
                Source = "login_footer_2.png"
                RelativeLayout.XConstraint=
                 "{ConstraintExpression Type=RelativeToParent,
                                        Property=Width,
                                        Factor=0}"
                RelativeLayout.YConstraint=
                 "{ConstraintExpression Type=RelativeToParent,
                                        Property=Height,
                                        Factor=1}" ></Image>
</RelativeLayout>

相对布局提供了一种非常简单的方法来做到这一点。

您应该使用约束类型 "relative to parent" 并使用因子来定位元素。

相对布局可以在使用因子定位时包含控件大小。这意味着如果您将系数设为 0,则图像的顶部将位于容器的顶部。

如果您将系数设置为 1,图像的 底部 将位于容器底部。

同样,系数 0.5 将使图像的中心位于容器的中心

因此,默认情况下,相对布局会在计算因子位置时处理您的控件尺寸。

您可以在官方文档中找到有关相关容器的更多信息和示例。示例项目中还包含代码示例来帮助您。

我发现一些东西可能对你有用。即使不是用RelativeLayout做的,用StackLayout也能得到你想要的。

想法是:

<StackLayout>
  <StackLayout  VerticalOptions="Start">
  <Image/> <!--top image-->
  </StackLayout>

  <StackLayout VerticalOptions="CenterAndExpand">
    <!-- middle controls -->
  </StackLayout>

  <StackLayout Orientation="Horizontal" VerticalOptions="End">
    <Image/> <!--bottom image-->
  </StackLayout>
</StackLayout>

通过扩展中间的 StackLayout,将剩余的空间分别推到顶部和底部。有一个固定的底部对我来说非常有用。