-
Notifications
You must be signed in to change notification settings - Fork 2
/
TraditionalRelativeLayout.xaml.cs
91 lines (77 loc) · 3.49 KB
/
TraditionalRelativeLayout.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ReSharper disable CompareOfFloatsByEqualityOperator
using EasyLayout.Forms;
using EasyLayout.Forms.Sample;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace EasyLayout.Sample.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TraditionalRelativeLayout
{
private Label _mainLabel;
private Label _relativeLabel;
private Label _centerLabel;
public TraditionalRelativeLayout ()
{
InitializeComponent ();
Title = "Traditional XAML RelativeLayout";
AddViews();
ConstrainLayout();
}
private void ConstrainLayout()
{
var relativeLayout = EasyLayoutRelativeLayout;
relativeLayout.ConstrainLayout(() =>
_mainLabel.Bounds.Top == relativeLayout.Bounds.Top + 10 &&
_mainLabel.Bounds.Left == relativeLayout.Bounds.Left + 10 &&
_mainLabel.Bounds.Width == 150 &&
_mainLabel.Bounds.Height == 40 &&
_relativeLabel.Bounds.Top == _mainLabel.Bounds.Bottom + 10 &&
_relativeLabel.Bounds.Left == _mainLabel.Bounds.Right + 10 &&
_relativeLabel.Bounds.Width == _mainLabel.Bounds.Width &&
_relativeLabel.Bounds.Height == _mainLabel.Bounds.Height &&
_centerLabel.Bounds.GetCenterX() == _relativeLabel.Bounds.GetCenterX() &&
_centerLabel.Bounds.Top == _relativeLabel.Bounds.Bottom + 10
);
}
private void AddViews()
{
AddTraditionalProgramaticRelativeLayout();
AddEasyLayoutControls();
}
private void AddEasyLayoutControls()
{
var relativeLayout = EasyLayoutRelativeLayout;
_mainLabel = relativeLayout.AddLabel("MainLabel", Colors.DarkGrey, Colors.BluePurple);
_relativeLabel = relativeLayout.AddLabel("RelativeLabel", Colors.DarkGrey, Colors.Red);
_centerLabel = relativeLayout.AddLabel("CenterLabel", Colors.White, Colors.Green);
}
private void AddTraditionalProgramaticRelativeLayout()
{
var relativeLayout = InnerRelativeLayout;
var mainLabel = relativeLayout.AddLabel("MainLabel", Colors.DarkGrey, Colors.BluePurple);
var relativeLabel = relativeLayout.AddLabel("RelativeLabel", Colors.DarkGrey, Colors.Red);
var centerLabel = relativeLayout.AddLabel("CenterLabel", Colors.White, Colors.Green);
relativeLayout.Children.Add(mainLabel,
Constraint.RelativeToParent(rl => rl.X + 10),
Constraint.RelativeToParent(rl => rl.Y + 10),
Constraint.Constant(150),
Constraint.Constant(40)
);
relativeLayout.Children.Add(relativeLabel,
Constraint.RelativeToView(mainLabel, (rl, v) => v.X + v.Width + 10),
Constraint.RelativeToView(mainLabel, (rl, v) => v.Y + v.Height + 10),
Constraint.RelativeToView(mainLabel, (rl, v) => v.Width),
Constraint.RelativeToView(mainLabel, (rl, v) => v.Height)
);
relativeLayout.Children.Add(centerLabel,
Constraint.RelativeToView(relativeLabel,
(rl, v) => v.X + (v.Width * .5f) - (GetSize(centerLabel, rl).Width / 2)),
Constraint.RelativeToView(relativeLabel, (rl, v) => v.Y + v.Height + 10)
//,Constraint.RelativeToView(mainLabel, (rl, v) => v.Width)
// ^ this breaks RelativeLayout
);
}
private Size GetSize(VisualElement ve, RelativeLayout rl) => ve.Measure(rl.Width, rl.Height).Request;
}
}