我已经阅读了很多关于相对布局的文档,但我没有得到Constant和Factor的确切含义。可以解释一下吗?
答案 0 :(得分:9)
你说你已经阅读了很多文档,但我认为如果你仔细观察,RelativeLayout documentation会给出一个很好的例子:
这个XAML:
<BoxView Color="Green" WidthRequest="50" HeightRequest="50"
RelativeLayout.XConstraint =
"{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0.5,
Constant=-100}"
RelativeLayout.YConstraint =
"{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0.5,
Constant=-100}" />
与此C#相同:
layout.Children.Add(box, Constraint.RelativeToParent((parent) =>
{
return (.5 * parent.Width) - 100;
}),
Constraint.RelativeToParent((parent) =>
{
return (.5 * parent.Height) - 100;
}),
Constraint.Constant(50), Constraint.Constant(50));
如果你看一下C#代码,你可能会更清楚:
Factor
是值乘以的因素Constant
是一个偏移量,在与因子相乘后将与您的值相加所以这是公式:
(因子*值)+常数
一个例子:
这将导致:(0.5 * 300) - 100 = 50