2 using System.Threading;
4 using Tizen.NUI.BaseComponents;
5 using System.Collections.Generic;
7 namespace CustomLayoutByAbsoluteLayout
11 public static string resources = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
12 public static readonly string[] s_images = new string[]
14 resources + "images/gallery-1.jpg",
15 resources + "images/gallery-2.jpg",
16 resources + "images/gallery-3.jpg",
17 resources + "images/gallery-4.jpg",
18 resources + "images/image-1.jpg",
19 resources + "images/image-2.jpg",
20 resources + "images/image-3.jpg",
24 public class CustomLayoutHorizental : LayoutGroup
26 private static LayoutItem[] childLayouts = new LayoutItem[10];
28 public CustomLayoutHorizental()
30 Console.WriteLine($"CustomLayoutHorizental() constructor!");
32 protected override void OnMeasure(LayoutMeasureSpec widthMeasureSpec, LayoutMeasureSpec heightMeasureSpec)
34 Console.WriteLine($"CustomLayoutHorizental OnMeasure() START");
36 var accumulatedWidth = new LayoutLength(0);
37 var maxHeight = new LayoutLength(0);
39 // this is needed, otherwise the child's LayoutItem is garbage collected!
40 for (uint i = 0; i < ChildCount; ++i)
42 childLayouts[i] = GetChildAt(i);
45 // In this layout we will:
46 // Measuring the layout with the children in a horizontal configuration, one after another
47 // Set the required width to be the accumulated width of our children
48 // Set the required height to be the maximum height of any of our children
49 for (uint i = 0; i < ChildCount; ++i)
51 var childLayout = childLayouts[i];
53 Console.WriteLine($"child count={ChildCount}, i={i}");
56 MeasureChild(childLayout, widthMeasureSpec, heightMeasureSpec);
57 accumulatedWidth += childLayout.MeasuredWidth;
58 maxHeight.Value = System.Math.Max(childLayout.MeasuredHeight.Value, maxHeight.Value);
59 Console.WriteLine($"child layout is not NULL! accumulatedWidth={accumulatedWidth.Value}, i={i}");
62 // Finally, call this method to set the dimensions we would like
63 SetMeasuredDimensions(new MeasuredSize(accumulatedWidth), new MeasuredSize(maxHeight));
64 Console.WriteLine($"CustomLayoutHorizental OnMeasure() accumlated width={accumulatedWidth.Value}, maxHeight={maxHeight.Value} END");
66 protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
68 Console.WriteLine($"CustomLayoutHorizental OnLayout() START");
70 LayoutLength childTop = new LayoutLength(0);
71 LayoutLength childLeft = new LayoutLength(0);
73 // We want to vertically align the children to the middle
74 var height = bottom - top;
75 var middle = height / 2;
77 // Horizontally align the children to the middle of the space they are given too
78 var width = right - left;
79 uint count = ChildCount;
80 Console.WriteLine($"child count={count}");
82 var childIncrement = 0;
85 childIncrement = width.Value / System.Convert.ToInt32(count);
87 var center = childIncrement / 2;
89 // Check layout direction
90 var view = GetOwner();
91 ViewLayoutDirectionType layoutDirection = view.LayoutDirection;
93 // this is needed, otherwise the child's LayoutItem is garbage collected!
94 for (uint i = 0; i < ChildCount; ++i)
96 childLayouts[i] = GetChildAt(i);
99 for (uint i = 0; i < count; i++)
102 // If RTL, then layout the last item first
103 if (layoutDirection == ViewLayoutDirectionType.RTL)
105 itemIndex = count - 1 - i;
112 var childLayout = childLayouts[itemIndex];
115 var childWidth = childLayout.MeasuredWidth;
116 var childHeight = childLayout.MeasuredHeight;
118 childTop = middle - (childHeight / 2);
120 var leftPosition = childLeft + center - childWidth / 2;
122 childLayout.Layout(leftPosition, childTop, leftPosition + childWidth, childTop + childHeight);
123 childLeft += childIncrement;
125 Console.WriteLine($"child layout is not NULL! childWidth={childWidth.Value}, i={i}");
128 Console.WriteLine($"CustomLayoutHorizental OnLayout() END");
132 public class CustomLayoutVertical : LayoutGroup
134 public CustomLayoutVertical()
136 this.LayoutAnimate = true;
139 private static LayoutItem[] childLayouts = new LayoutItem[10];
141 protected override void OnMeasure(LayoutMeasureSpec widthMeasureSpec, LayoutMeasureSpec heightMeasureSpec)
143 var accumulatedHeight = new LayoutLength(0);
144 var maxWidth = new LayoutLength(0);
146 for (uint i = 0; i < ChildCount; ++i)
148 childLayouts[i] = GetChildAt(i);
151 for (uint i = 0; i < ChildCount; ++i)
153 var childLayout = childLayouts[i];
156 MeasureChild(childLayout, widthMeasureSpec, heightMeasureSpec);
157 accumulatedHeight += childLayout.MeasuredHeight;
158 maxWidth.Value = System.Math.Max(childLayout.MeasuredWidth.Value, maxWidth.Value);
159 Console.WriteLine($"CustomLayoutVertical child layout is not NULL! accumulatedHeight={accumulatedHeight.Value}, i={i}");
162 SetMeasuredDimensions(new MeasuredSize(maxWidth), new MeasuredSize(accumulatedHeight));
163 Console.WriteLine($"CustomLayoutVertical OnMeasure() max width={maxWidth.Value}, accumulated Height={accumulatedHeight.Value}");
166 protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
168 LayoutLength childTop = new LayoutLength(0);
169 LayoutLength childLeft = new LayoutLength(0);
171 var height = bottom - top;
172 var width = right - left;
173 var middle = width / 2;
175 uint count = ChildCount;
176 var childIncrement = 0;
179 childIncrement = height.Value / System.Convert.ToInt32(count);
181 var center = childIncrement / 2;
183 var view = GetOwner();
184 ViewLayoutDirectionType layoutDirection = view.LayoutDirection;
186 for (uint i = 0; i < ChildCount; ++i)
188 childLayouts[i] = GetChildAt(i);
191 for (uint i = 0; i < count; i++)
194 if (layoutDirection == ViewLayoutDirectionType.RTL)
196 itemIndex = count - 1 - i;
203 var childLayout = childLayouts[itemIndex];
206 var childWidth = childLayout.MeasuredWidth;
207 var childHeight = childLayout.MeasuredHeight;
209 childLeft = middle - (childWidth / 2);
211 var topPosition = childTop + center - childHeight / 2;
213 childLayout.Layout(childLeft, topPosition, childLeft + childWidth, topPosition + childHeight);
214 childTop += childIncrement;
216 Console.WriteLine($"CustomLayoutVertical child layout is not NULL! childWidth={childWidth.Value}, i={i}");
219 Console.WriteLine($"CustomLayoutVertical OnLayout() END");
223 class Example : NUIApplication
225 public Example() : base()
227 Console.WriteLine("Example()!");
230 protected override void OnCreate()
236 static View rootLayoutView, linearContainer;
237 const int MAX_CHILDREN = 7;
238 static ImageView[] imageViews = new ImageView[MAX_CHILDREN];
239 static CustomLayoutHorizental horizontalLayout;
240 static CustomLayoutVertical verticalLayout;
241 static AbsoluteLayout rootLayout;
243 private void Initialize()
245 Console.WriteLine("Initialize()!");
246 Window window = Window.Instance;
247 window.BackgroundColor = Color.Green;
249 rootLayoutView = new View();
250 rootLayoutView.WidthSpecificationFixed = 1900;
251 rootLayoutView.HeightSpecificationFixed = 1000;
252 rootLayoutView.Position = new Position(0, 0, 0);
253 rootLayoutView.BackgroundColor = Color.Magenta;
255 linearContainer = new View();
256 linearContainer.PositionUsesPivotPoint = true;
257 linearContainer.PivotPoint = PivotPoint.Center;
258 linearContainer.ParentOrigin = ParentOrigin.Center;
259 //linearContainer.BackgroundColor = Color.Yellow;
260 linearContainer.KeyEvent += OnKeyEvent;
261 linearContainer.Focusable = true;
263 for (int index = 0; index < MAX_CHILDREN - 3; index++)
265 imageViews[index] = new ImageView(Images.s_images[index]);
266 imageViews[index].WidthSpecificationFixed = 150;
267 imageViews[index].HeightSpecificationFixed = 100;
268 linearContainer.Add(imageViews[index]);
270 for (int index = MAX_CHILDREN - 3; index < MAX_CHILDREN; index++)
272 imageViews[index] = new ImageView(Images.s_images[index]);
273 imageViews[index].WidthSpecificationFixed = 150;
274 imageViews[index].HeightSpecificationFixed = 100;
275 imageViews[index].Name = "t_image" + (index - 3);
278 horizontalLayout = new CustomLayoutHorizental();
279 verticalLayout = new CustomLayoutVertical();
280 horizontalLayout.LayoutAnimate = true;
281 linearContainer.Layout = horizontalLayout;
283 rootLayout = new AbsoluteLayout();
284 rootLayoutView.Layout = rootLayout;
286 rootLayoutView.Add(linearContainer);
287 window.Add(rootLayoutView);
288 FocusManager.Instance.SetCurrentFocusView(linearContainer);
289 FocusManager.Instance.FocusIndicator = new View();
293 private bool OnKeyEvent(object source, View.KeyEventArgs e)
295 if (e.Key.State == Key.StateType.Down)
297 Console.WriteLine($"key pressed name={e.Key.KeyPressedName}");
298 switch (e.Key.KeyPressedName)
301 if (cnt1 < 4 && cnt1 > 0)
303 linearContainer.Add(imageViews[cnt1 + 3]);
309 if (cnt1 - 1 < 4 && cnt1 - 1 > 0)
311 View tmp = linearContainer.FindChildByName("t_image" + (cnt1 - 1));
314 linearContainer.Remove(tmp);
321 linearContainer.Layout = verticalLayout;
325 linearContainer.Layout = horizontalLayout;
329 if (linearContainer.LayoutDirection == ViewLayoutDirectionType.LTR) { linearContainer.LayoutDirection = ViewLayoutDirectionType.RTL; }
330 else { linearContainer.LayoutDirection = ViewLayoutDirectionType.LTR; }