Xamarin.Forms Guides | Samples

Xamarin.Forms.RelativeLayout Class

A Layout`1 that uses Constraints to layout its children.

See Also: RelativeLayout

Syntax

public class RelativeLayout : Layout<View>

Remarks

The RelativeLayoutExample class in the following code extends the ContentPage class by adding a RelativeLayout that contains a heading and another label. Both labels are positioned relative to the ContentPage:

C# Example

public class RelativeLayoutExample : ContentPage
{
    public RelativeLayoutExample ()
    {
        this.Padding = new Thickness (10, Device.OnPlatform (20, 0, 0), 10, 5);

        Label heading = new Label {
            Text = "RelativeLayout Example",
            TextColor = Color.Red,
        };

        Label relativelyPositioned = new Label {
            Text = "Positioned relative to my parent."
        };

        RelativeLayout relativeLayout = new RelativeLayout ();

        relativeLayout.Children.Add (heading, Constraint.RelativeToParent ((parent) => {
            return 0;
        }));

        relativeLayout.Children.Add (relativelyPositioned,
            Constraint.RelativeToParent ((parent) => {
                return parent.Width / 3;
            }),
            Constraint.RelativeToParent ((parent) => {
                return parent.Height / 2;
            }));
        this.Content = relativeLayout;
    }
}

For a more complete example that exercises many more of the layout options for the RelativeLayout class, see the FormsGallery sample that can be found on the Sample Applications page.

XAML for Xamarin.Forms supports the following attached properties for the RelativeLayout class:

AttachedPropertyValue
XConstraint

XAML markup extension for a constraint expression. See ConstraintExpression.

YConstraint

XAML markup extension for a constraint expression. See ConstraintExpression.

WidthConstraint

XAML markup extension for a constraint expression. See ConstraintExpression.

HeightConstraint

XAML markup extension for a constraint expression. See ConstraintExpression.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.RelativeLayout are listed below.

See Also: Inherited members from Xamarin.Forms.Layout<Xamarin.Forms.View>

Public Constructors

Creates a new RelativeLayout with default values.

Public Fields

static readonly
BoundsConstraintPropertyBindableProperty. Identifies the bindable property associated with RelativeLayout.GetBoundsConstraint/RelativeLayout.SetBoundsConstraint.
static readonly
HeightConstraintPropertyBindableProperty. Identifies the bindable property associated with the RelativeLayout.GetHeightConstraint / RelativeLayout.SetHeightConstraint methods.
static readonly
WidthConstraintPropertyBindableProperty. Identifies the width constraint.
static readonly
XConstraintPropertyBindableProperty. Identifies the constraint on X.
static readonly
YConstraintPropertyBindableProperty. Identifies the constraint on Y.

Public Properties

[read-only]
ChildrenRelativeLayout.IRelativeList<View>. List of Views that are children of this RelativeLayout.

Public Methods

static
GetBoundsConstraint(BindableObject) : BoundsConstraint
Returns the bounds constraint of the bindable.
static
GetHeightConstraint(BindableObject) : Constraint
Returns the height constraint of the bindable.
static
GetWidthConstraint(BindableObject) : Constraint
Returns the width constraint of the bindable.
static
GetXConstraint(BindableObject) : Constraint
Returns the X constraint of the bindable.
static
GetYConstraint(BindableObject) : Constraint
Returns the Y constraint of the bindable.
static
SetBoundsConstraint(BindableObject, BoundsConstraint)
Sets value as a constraint on the bounds of bindable.

Protected Methods

override
LayoutChildren(Double, Double, Double, Double)
Lays out the RelativeLayout.Children in the specified rectangle.
override
OnAdded(View)
Called when a View is added to the RelativeLayout.Children collection.
override
OnRemoved(View)
Called when a View is removed from the RelativeLayout.Children collection.
override
OnSizeRequest(Double, Double) : SizeRequest
Called when this RelativeLayout has received a size request.