3 using Tizen.NUI.BaseComponents;
5 namespace CustomLayoutWithoutAbsoluteLayout
7 public class CustomLayout : LayoutGroup
9 protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
11 var accumulatedWidth = new LayoutLength(0);
13 var measuredWidth = new LayoutLength(0);
14 LayoutLength measuredHeight = new LayoutLength(0);
15 MeasureSpecification.ModeType widthMode = widthMeasureSpec.Mode;
16 MeasureSpecification.ModeType heightMode = heightMeasureSpec.Mode;
18 bool isWidthExact = (widthMode == MeasureSpecification.ModeType.EXACTLY);
19 bool isHeightExact = (heightMode == MeasureSpecification.ModeType.EXACTLY);
21 // In this layout we will:
22 // Measuring the layout with the children in a horizontal configuration, one after another
23 // Set the required width to be the accumulated width of our children
24 // Set the required height to be the maximum height of any of our children
26 for (uint i = 0; i < ChildCount; ++i)
28 var childLayout = GetChildAt(i);
31 MeasureChild(childLayout, widthMeasureSpec, heightMeasureSpec);
32 accumulatedWidth += childLayout.MeasuredWidth;
33 maxHeight = System.Math.Max(childLayout.MeasuredHeight.Value, maxHeight);
37 measuredHeight.Value = maxHeight;
38 measuredWidth = accumulatedWidth;
42 measuredWidth = new LayoutLength(widthMeasureSpec.Size);
47 measuredHeight = new LayoutLength(heightMeasureSpec.Size);
50 // Finally, call this method to set the dimensions we would like
51 SetMeasuredDimensions(new MeasuredSize(measuredWidth), new MeasuredSize(measuredHeight));
54 protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
56 LayoutLength childTop = new LayoutLength(0);
57 LayoutLength childLeft = new LayoutLength(0);
59 // We want to vertically align the children to the middle
60 var height = bottom - top;
61 var middle = height / 2;
63 // Horizontally align the children to the middle of the space they are given too
64 var width = right - left;
65 uint count = ChildCount;
66 var childIncrement = 0;
69 childIncrement = width.Value / System.Convert.ToInt32(count);
71 var center = childIncrement / 2;
73 // Check layout direction
74 var view = GetOwner();
75 ViewLayoutDirectionType layoutDirection = view.LayoutDirection;
77 for (uint i = 0; i < count; i++)
80 // If RTL, then layout the last item first
81 if (layoutDirection == ViewLayoutDirectionType.RTL)
83 itemIndex = count - 1 - i;
90 LayoutItem childLayout = GetChildAt(itemIndex);
93 var childWidth = childLayout.MeasuredWidth;
94 var childHeight = childLayout.MeasuredHeight;
96 childTop = middle - (childHeight / 2);
98 var leftPosition = childLeft + center - childWidth / 2;
100 childLayout.Layout(leftPosition, childTop, leftPosition + childWidth, childTop + childHeight);
101 childLeft += childIncrement;
108 namespace CustomLayoutWithoutAbsoluteLayout
110 static class TestImages
112 //private const string resources = "./res";
113 public static string resources = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
114 /// Child image filenames
115 public static readonly string[] s_images = new string[]
117 resources + "/images/application-icon-101.png",
118 resources + "/images/application-icon-102.png",
119 resources + "/images/application-icon-103.png",
120 resources + "/images/application-icon-104.png"
124 class Example : NUIApplication
126 protected override void OnCreate()
132 private void Initialize()
134 // Change the background color of Window to White
135 Window window = Window.Instance;
136 window.BackgroundColor = Color.White;
138 //Layer layer = new Layer();
140 //window.AddLayer(layer);
143 View customLayoutView = new View();
144 customLayoutView.Name = "CustomLayoutView";
145 customLayoutView.ParentOrigin = ParentOrigin.Center;
146 customLayoutView.PivotPoint = PivotPoint.Center;
147 customLayoutView.PositionUsesPivotPoint = true;
148 // Set our Custom Layout on the view
149 var layout = new CustomLayout();
150 customLayoutView.Layout = layout;
151 customLayoutView.SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new PropertyValue(-2)); // -2 WRAP_CONTENT
152 customLayoutView.SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new PropertyValue(350));
153 customLayoutView.BackgroundColor = Color.Blue;
154 window.Add(customLayoutView);
156 // Add child image-views to the created view
157 foreach (String image in TestImages.s_images)
159 customLayoutView.Add(CreateChildImageView(image, new Size2D(100, 100)));
164 /// Helper function to create ImageViews with given filename and size..<br />
166 /// <param name="filename"> The filename of the image to use.</param>
167 /// <param name="size"> The size that the image should be loaded at.</param>
168 /// <returns>The created ImageView.</returns>
169 ImageView CreateChildImageView(String url, Size2D size)
171 ImageView imageView = new ImageView();
172 ImageVisual imageVisual = new ImageVisual();
174 imageVisual.URL = url;
175 imageVisual.DesiredHeight = size.Height;
176 imageVisual.DesiredWidth = size.Width;
177 imageView.Image = imageVisual.OutputVisualMap;
179 imageView.Name = "ImageView";
180 imageView.HeightResizePolicy = ResizePolicyType.Fixed;
181 imageView.WidthResizePolicy = ResizePolicyType.Fixed;
185 static void _Main(string[] args)
187 Example simpleLayout = new Example();
188 simpleLayout.Run(args);