protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom) { LayoutLength childTop = new LayoutLength(0); LayoutLength childLeft = new LayoutLength(0); // We want to vertically align the children to the middle var height = bottom - top; var middle = height / 2; // Horizontally align the children to the middle of the space they are given too var width = right - left; uint count = ChildCount; var childIncrement = 0; if (count > 0) { childIncrement = width.Value / System.Convert.ToInt32(count); } var center = childIncrement / 2; // Check layout direction var view = GetOwner(); ViewLayoutDirectionType layoutDirection = view.LayoutDirection; for (uint i = 0; i < count; i++) { uint itemIndex; // If RTL, then layout the last item first if (layoutDirection == ViewLayoutDirectionType.RTL) { itemIndex = count - 1 - i; } else { itemIndex = i; } LayoutItem childLayout = GetChildAt(itemIndex); if (childLayout) { var childWidth = childLayout.MeasuredWidth; var childHeight = childLayout.MeasuredHeight; childTop = middle - (childHeight / 2); var leftPosition = childLeft + center - childWidth / 2; childLayout.Layout(leftPosition, childTop, leftPosition + childWidth, childTop + childHeight); childLeft += childIncrement; } } }
protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom) { LayoutLength childLeft = new LayoutLength(0); // We want to vertically align the children to the middle LayoutLength height = bottom - top; float middle = height.AsDecimal() / 2; // Horizontally align the children to the middle of the space they are given too LayoutLength width = right - left; int count = LayoutChildren.Count; int childIncrement = 0; if (count > 0) { childIncrement = (int)System.Math.Ceiling(width.AsDecimal() / count); } float center = childIncrement / 2; // Check layout direction var view = Owner; ViewLayoutDirectionType layoutDirection = view.LayoutDirection; for (int i = 0; i < count; i++) { int itemIndex = i; // If RTL, then layout the last item first if (layoutDirection == ViewLayoutDirectionType.RTL) { itemIndex = count - 1 - i; } LayoutItem childLayout = LayoutChildren[itemIndex]; if (childLayout != null) { LayoutLength childWidth = childLayout.MeasuredWidth.Size; LayoutLength childHeight = childLayout.MeasuredHeight.Size; LayoutLength childTop = new LayoutLength(middle - (childHeight.AsDecimal() / 2)); LayoutLength leftPosition = new LayoutLength(childLeft.AsDecimal() + center - childWidth.AsDecimal() / 2); childLayout.Layout(leftPosition, childTop, leftPosition + childWidth, childTop + childHeight); childLeft += new LayoutLength(childIncrement); } } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var metrics = new DisplayMetrics(); WindowManager.DefaultDisplay.GetMetrics(metrics); float spacing = 40; var root = new LayoutItem(this) { Width = metrics.WidthPixels, Height = metrics.HeightPixels, PaddingBottom = 80 }; var label = new Item <TextView>(this) { Margin = spacing }; label.View.Text = "This is the list of items you have added. You can add new items and clear the list using the buttons at the bottom. This label has extra lines on purpose."; label.SelfSizing = delegate(Xamarin.Flex.Item item, ref float width, ref float height) { height = new StaticLayout(label.View.Text, label.View.Paint, (int)width, Layout.Alignment.AlignNormal, 1, 0, true).Height; }; root.Add(label); var list = new Item <ListView>(this) { Grow = 1, MarginLeft = spacing, MarginRight = spacing }; list.View.Adapter = new ArrayAdapter(this, Resource.Layout.TextViewItem); root.Add(list); var input = new Item <EditText>(this) { Margin = spacing }; input.View.Hint = "Enter list item"; input.Height = 80; root.Add(input); var buttons_row = new LayoutItem(this) { Direction = Xamarin.Flex.Direction.Row, Height = 80, MarginLeft = spacing, MarginRight = spacing, MarginBottom = spacing }; root.Add(buttons_row); var add_button = new Item <Button>(this) { Grow = 1, Height = 80 }; add_button.View.Text = "Add"; add_button.View.Click += delegate { var adapter = (ArrayAdapter)list.View.Adapter; if (input.View.Text != "") { adapter.Add(input.View.Text); input.View.Text = ""; } }; buttons_row.Add(add_button); var clear_button = new Item <Button>(this) { Grow = 1, Height = 80 }; clear_button.View.Text = "Clear"; clear_button.View.Click += delegate { var adapter = (ArrayAdapter)list.View.Adapter; adapter.Clear(); }; buttons_row.Add(clear_button); root.Layout(); SetContentView(root.view); }