Xaml 设置X系数时,相对视图未将视图定位在水平中心。5
我正在做Xaml 设置X系数时,相对视图未将视图定位在水平中心。5,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我正在做RelativeLayout,我有三个BoxViewboxview3我想使用Type=RelativeToView在boxview2之后获取 boxview3的XConstraint我正在设置.5仍然boxview3显示在左上角为什么?如何在boxview 2之后获取boxview 3 <RelativeLayout> <BoxView x:Name="boxview1" BackgroundColor="#b87333"
RelativeLayout
,我有三个BoxView
boxview3
我想使用Type=RelativeToView
在boxview2
之后获取
boxview3
的XConstraint
我正在设置.5
仍然boxview3
显示在左上角为什么?如何在boxview 2
之后获取boxview 3
<RelativeLayout>
<BoxView x:Name="boxview1" BackgroundColor="#b87333"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,Factor=.5 }"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height, Factor=1}">
</BoxView>
<BoxView BackgroundColor="Red" x:Name="boxview2"
RelativeLayout.HeightConstraint="{ConstraintExpression ElementName=boxview1,
Type=RelativeToView,Property=Height,Factor=.1}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,
ElementName=boxview1,Factor=1,Property=Width}"
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,Property=Width, Factor=0.1,Constant=-10}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView,
ElementName=boxview1,Property=Height,Factor=.4}">
</BoxView>
<BoxView BackgroundColor="Blue" x:Name="boxview3"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,
ElementName=boxview2, Property=Width,Factor=.5}">
</BoxView>
</RelativeLayout>
注意:如果将XConstraint
设置为.5
我希望屏幕中间的视图是水平的
输出屏幕截图:
简而言之,您不能在Xaml中指定要查找的X约束,必须使用C。RelativeLayout中的所有元素都是相对于整个RelativeLayout定位的
您现在拥有的Xaml指定boxview2的宽度为0.1*RelativeLayout.width-10,而boxview3的X坐标为该宽度的一半,因此它将位于左上角0.05*RelativeLayout.width-5处,这就是您看到的位置
对于Xaml中的RelativeLayout约束,您可以使用视图左上角的X或Y中的1,或其宽度或高度。要实现所需功能,您需要boxview2(或boxview2.X+boxview2.Width)的右上角。您必须在C代码隐藏中创建boxview3,如:
reelativeLayout.Children.Add (boxview3, Constraint.RelativeToView (boxview2, (parent, view) => {
return view.X + view.Width;
}),
… // other constraints
));
根据您的需要,您可能会找到一个更容易使用的不同容器。您的问题究竟出在哪里还不清楚。你想让你的蓝色boxview在这里@丹斯拉布-是的。我希望使用Type=RelativeToView
将蓝色框置于红色框的右侧,仅此而已。当我给X as.5时,我原以为蓝色的盒子会在中间,但事实并非如此。非常感谢。