【问题标题】:Use of Constant and Factor in Xamarin.Forms RelativeLayout?在 Xamarin.Forms RelativeLayout 中使用常量和因子?
【发布时间】:2016-12-20 15:59:42
【问题描述】:

我已经阅读了很多关于相对布局的文档,但我没有得到常量和因子的确切含义。有人可以解释一下吗?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    您说您已经阅读了很多文档,但如果您仔细观察,我认为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 是一个偏移量,在乘以因子后将与您的值相加

    所以这是公式:

    (因子 * 值)+ 常数

    一个例子:

    • 值 = 300
    • 系数 = 0.5
    • 常数 = - 100

    这将导致:(0.5 * 300) - 100 = 50

    【讨论】:

    • 那么如何设置因子和常数呢?
    • 您使用FactorConstant 属性设置它(如您在文档和我的回答中的XAML/C# 中所见)
    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 2014-12-18
    • 2019-02-04
    • 2016-07-09
    • 2021-11-23
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多