Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Constant and Factor in Xamarin.Forms RelativeLayout?

I have read a lot of documentation on Relative Layout but I am not getting exact meaning of Constant and Factor.Can Anybody explain?

like image 598
Adit Kothari Avatar asked Dec 24 '22 22:12

Adit Kothari


1 Answers

You said you've read a lot of documentation, but I think the RelativeLayout documentation gives a good example if you look closer:

This 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}" />

Does the same as this 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));

If you look at the C# code, things may be clearer for you:

  • Factor is the factor with which the value will be multiplied
  • Constant is an offset which will be summed up to your value after it has been multiplied with the factor

So this is the formula:

(factor * value) + constant

An example:

  • Value = 300
  • Factor = 0.5
  • Constant = - 100

This will result in: (0.5 * 300) - 100 = 50

like image 70
Dennis Schröer Avatar answered Jan 11 '23 18:01

Dennis Schröer