Xamarin.forms 如何在Xamarin的RelativeLayout中定位元素?
关于我的问题,有没有办法将用户名和密码Xamarin.forms 如何在Xamarin的RelativeLayout中定位元素?,xamarin.forms,android-relativelayout,Xamarin.forms,Android Relativelayout,关于我的问题,有没有办法将用户名和密码条目元素放在欢迎标签的正下方 因此,基本上,该应用程序应该如下所示: [Welcome Label] [username] [password] [login] 我试着用 <RelativeLayout> <Label Text="Welcome" BackgroundColor="Yellow" TextCol
条目
元素放在欢迎标签的正下方
因此,基本上,该应用程序应该如下所示:
[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,只是为了参考。您可以使用属性来指示与视图相关的约束
<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>
效果如下:
如果不指定任何约束,它只会堆叠元素。StackLayout或网格可能对您的用例更有意义如果您是新手,我建议您从StackLayout开始,它非常有用simpler@Jason嗯。。。那么我将如何实现中心对齐?然后使用网格。理解。只有一个问题。我们如何选择一个合适的常数添加到因子中?为什么选择Property=X而不是Width(如Property=Width)?@d4rk4ng31这取决于您的需要,例如,您想要一个特定的常数偏移,您可以设置为常数,如10,20,30…如果您想要一个百分比偏差,您可以设置为因子,如0.5(50%),0,33(33%)…@d4rk4ng31 First XConstraint用于约束条目在X轴上的位置,然后RelativeToView和Property=Width表示偏移值为标签宽度的50%,您可以看到条目将从标签宽度的一半开始定位。@d4rk4ng31是的,您可以在中阅读更多内容。有标记扩展名的说明。