Xamarin表格 - 相对布局 - 将第一个视图的顶部与第二个底部对齐,而不将它们添加到垂直StackLayout中

时间:2017-06-29 08:55:04

标签: xamarin xamarin.forms relativelayout

我需要在相对布局中添加视图。

我需要将它们相对于彼此的边缘垂直/水平对齐。与父视图无关。

但似乎Xamarin相对布局API只能提供相对于父RelativeToParent的相对布局对齐。

2 个答案:

答案 0 :(得分:1)

xamarin表单支持RelativeToView

    <RelativeLayout>
    <BoxView x:Name="topBox" 
             Color="Red"
             RelativeLayout.WidthConstraint ="{ConstraintExpression
                  Type=RelativeToParent,
                  Property=Width,
                  Factor=0.6,
                  Constant=0}"
             RelativeLayout.XConstraint ="{ConstraintExpression
                  Type=RelativeToParent,
                  Property=X,
                  Constant=20}" />

    <BoxView 
        Color="Blue"
        RelativeLayout.WidthConstraint ="{ConstraintExpression
            Type=RelativeToParent,
            Property=Width,
            Factor=0.6,
            Constant=0}"
        RelativeLayout.YConstraint="{ConstraintExpression
            Type=RelativeToView,
            ElementName=topBox,
            Property=Y,
            Constant=40}"
        RelativeLayout.XConstraint="{ConstraintExpression
            Type=RelativeToView,
            ElementName=topBox,
            Property=X,
            Constant=0}" /> 
</RelativeLayout>

enter image description here

希望有所帮助

答案 1 :(得分:0)

Kojan指出,Constraint.RelativeToView

对于XAML和C#示例,请查看: https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/relative-layout/#Understanding_Constraints