Advertisement
smeacham

Untitled

May 17th, 2016
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using Xamarin.Forms;
  2.  
  3. namespace RelativeLayoutExample
  4. {
  5.     public class MyContentPage : ContentPage
  6.     {
  7.         public MyContentPage()
  8.         {
  9.             RelativeLayout relativeLayout = new RelativeLayout();
  10.  
  11.             Label upperLeft = new Label
  12.             {
  13.                 Text = "Upper Left",
  14.                 FontSize = 20
  15.             };
  16.  
  17.             relativeLayout.Children.Add(
  18.                 upperLeft,
  19.                 Constraint.Constant(0),
  20.                 Constraint.Constant(0));
  21.  
  22.             Label constantLabel = new Label
  23.             {
  24.                 Text = "Constants are Absolute",
  25.                 FontSize = 20
  26.             };
  27.  
  28.             relativeLayout.Children.Add(
  29.                 constantLabel,
  30.                 Constraint.Constant(100),
  31.                 Constraint.Constant(100),
  32.                 Constraint.Constant(50),
  33.                 Constraint.Constant(200));
  34.  
  35.             Label halfwayDown = new Label
  36.             {
  37.                 Text = "Halfway down and across",
  38.                 FontSize = 15
  39.             };
  40.  
  41.             relativeLayout.Children.Add(
  42.                 halfwayDown,
  43.                 Constraint.RelativeToParent(
  44.                     (Parent) =>
  45.                     {
  46.                         return Parent.Width / 2;
  47.                     }),
  48.                 Constraint.RelativeToParent(
  49.                     (Parent) =>
  50.                     {
  51.                         return Parent.Height / 2;
  52.                     })
  53.                 );
  54.  
  55.             BoxView boxView = new BoxView
  56.             {
  57.                 Color = Color.Accent,
  58.                 WidthRequest = 150,
  59.                 HeightRequest = 150,
  60.                 HorizontalOptions = LayoutOptions.Center,
  61.                 VerticalOptions = LayoutOptions.CenterAndExpand
  62.             };
  63.  
  64.             relativeLayout.Children.Add(
  65.                 boxView,
  66.                 Constraint.Constant(0),             // X
  67.                 Constraint.RelativeToParent(        // Y
  68.                     (Parent) =>
  69.                     {
  70.                         return Parent.Height / 2;
  71.                     }
  72.                 ),
  73.                 Constraint.RelativeToParent(        // width
  74.                     (Parent) =>
  75.                     {
  76.                         return Parent.Width / 2;
  77.                     }),
  78.                 Constraint.RelativeToParent(        // height
  79.                     (Parent) =>
  80.                     {
  81.                         return Parent.Height / 2;
  82.                     })
  83.                 );
  84.  
  85.             Content = relativeLayout;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement