void SetRelativeLayoutConstraints()
        {
            circleLayout.SetConstraints(topLevelLayout,
                                        xConstraint: ExposedConstraint.RelativeToParent((parent) => { return(parent.Width / 2 - 37.5); }),
                                        yConstraint: ExposedConstraint.RelativeToView(mainFrame, (parent, sibling) => { return(mainFrame.Y - 37.5); }));

            defaultLayout.SetConstraints(circleLayout,
                                         ExposedConstraint.RelativeToView(outerCircle, (parent, sibling) => { return(outerCircle.X + 5); }),
                                         ExposedConstraint.RelativeToView(outerCircle, (parent, sibling) => { return(outerCircle.Y + 5); }),
                                         ExposedConstraint.RelativeToParent((parent) => { return(parent.Width - 10); }),
                                         ExposedConstraint.RelativeToParent((parent) => { return(parent.Height - 10); }));
        }
Beispiel #2
0
        /// <summary>
        /// Alternative method for defining constraints within a <see cref="RelativeLayout"/> using <see cref="ExposedConstraint"/>
        /// </summary>
        /// <param name="relativeLayout"><see cref="RelativeLayout"/> to add the <paramref name="view"/> to.</param>
        /// <param name="xConstraint">X constraint.</param>
        /// <param name="yConstraint">Y constraint.</param>
        /// <param name="widthConstraint">Width constraint.</param>
        /// <param name="heightConstraint">Height constraint.</param>
        public static void SetConstraints(this View view, RelativeLayout relativeLayout,
                                          ExposedConstraint xConstraint     = null, ExposedConstraint yConstraint      = null,
                                          ExposedConstraint widthConstraint = null, ExposedConstraint heightConstraint = null)
        {
            var parents = new List <View>();

            Func <double> x;

            if (xConstraint != null)
            {
                x = () => xConstraint.Compute(relativeLayout);
                if (xConstraint.RelativeTo != null)
                {
                    parents.AddRange(xConstraint.RelativeTo);
                }
            }
            else
            {
                x = () => 0;
            }

            Func <double> y;

            if (yConstraint != null)
            {
                y = () => yConstraint.Compute(relativeLayout);
                if (yConstraint.RelativeTo != null)
                {
                    parents.AddRange(yConstraint.RelativeTo);
                }
            }
            else
            {
                y = () => 0;
            }

            Func <double> width;

            if (widthConstraint != null)
            {
                width = () => widthConstraint.Compute(relativeLayout);
                if (widthConstraint.RelativeTo != null)
                {
                    parents.AddRange(widthConstraint.RelativeTo);
                }
            }
            else
            {
                width = () => view.Measure(relativeLayout.Width, relativeLayout.Height, MeasureFlags.IncludeMargins).Request.Width;
            }

            Func <double> height;

            if (heightConstraint != null)
            {
                height = () => heightConstraint.Compute(relativeLayout);
                if (heightConstraint.RelativeTo != null)
                {
                    parents.AddRange(heightConstraint.RelativeTo);
                }
            }
            else
            {
                height = () => view.Measure(relativeLayout.Width, relativeLayout.Height, MeasureFlags.IncludeMargins).Request.Height;
            }

            BoundsConstraint bounds = BoundsConstraint.FromExpression(() => new Rectangle(x(), y(), width(), height()), parents.Distinct().ToArray());

            RelativeLayout.SetBoundsConstraint(view, bounds);
        }