Xamarin 中 RelativeLayout 中的一个位置元素如何?

How does one position elements in RelativeLayout in Xamarin?

关于我的问题 ,有什么方法可以将用户名和密码 entry 元素放在欢迎标签的正下方?

所以,基本上,该应用应该如下所示:

[Welcome Label]




  [username]
  [password]


   [login]

我试过使用

<RelativeLayout>
    <Label
        Text="Welcome" 
        BackgroundColor="Yellow" 
        TextColor="Green" 
        FontSize="Medium"
        VerticalTextAlignment="Center"
        HorizontalTextAlignment="Center"
        RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Factor=1, Property=Width, Constant=0}"
        RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Factor=0.1, Property=Height, Constant=0}"
    />

    <Entry
        Text="Username"
        IsPassword="False"
    />
</RelativeLayout>

(目前只有用户名字段)但无济于事。为什么将输入字段放在标签上?

尝试制作类似这样的东西并根据您的需要更改对齐方式。

            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                <Label Text="Welcome" HorizontalOptions="Start" Margin="10, 30, 0, 0"/>
                <Entry Placeholder="User Id" Margin="10, 20, 0, 0"/>
                <Entry Placeholder="Password" IsPassword="True" Margin="10"/>
                <Button Text="Login" HorizontalOptions="Center"/>
            </StackLayout>

我使用堆栈布局只是为了基本目的,它并不完美UI但仅供参考。

您可以使用 RelativeToView 属性 来指示与视图相关的约束

<RelativeLayout>
        <Label
            x:Name="label"
            Text="Welcome" 
            BackgroundColor="Yellow" 
            TextColor="Green" 
            FontSize="Medium"
            WidthRequest="100"
            VerticalTextAlignment="Center"
            HorizontalTextAlignment="Center"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Factor=1, Property=Width, Constant=1}"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Factor=0.1, Property=Height, Constant=0}"
            />

        <Entry
            x:Name="name"
            Text="Username"
            IsPassword="False"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=label,Property=Width,Factor=0.50,Constant=-50}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=label,Property=Y ,Constant=200}"

            />
        <Entry
            x:Name="password"
            Text="Password"
            IsPassword="False"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=name,Property=X}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=name,Property=Y ,Constant=50}"
            />
        <Button Text="Login" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=password,Property=X}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=password,Property=Y ,Constant=200}"/>

</RelativeLayout>

效果如: