|
| 1 | +--- |
| 2 | +title: The Layout Process |
| 3 | +--- |
| 4 | + |
| 5 | +The ability for all your views to render with the right dimensions and positions requires a run of "the layout process". When rendering, a recursive process runs through every view in the **view hierarchy** in two passes — a measure pass and a layout pass. |
| 6 | + |
| 7 | +During **the measure pass** every view is measured to obtain its desired size. The following properties are considered during the measuring: |
| 8 | + |
| 9 | +- `width`, `height` |
| 10 | +- `minWidth`, `minHeight` |
| 11 | +- `visibility` |
| 12 | +- `marginTop`, `marginRight`, `marginBottom`, `marginLeft` |
| 13 | + |
| 14 | +During **the layout pass** every view is placed in a specific layout slot. The slot is determined by the result of the measure pass and the following properties: |
| 15 | + |
| 16 | +- `marginTop`, `marginRight`, `marginBottom`, `marginLeft` |
| 17 | +- `horizontalAlignment`, `verticalAlignment` |
| 18 | + |
| 19 | +The layout process is by nature an resource-intensive process and it's performance highly depends on the number views (and complexity). |
| 20 | +<!-- TODO: fix links --> |
| 21 | +:::tip |
| 22 | +It's a good practice to minimize deep nesting of views. Instead, utilize different [Layout Containers](/ui/#layout-containers) to achieve a simpler and more organized view hierarchy. This approach improves readability, maintainability and performance. |
| 23 | + |
| 24 | +**For example:** don't treat `<StackLayout>` as a `<div>` — instead try to use a `<GridLayout>` with specific `rows` and `columns` to achieve the same result: |
| 25 | + |
| 26 | +- Bad Practice: |
| 27 | + |
| 28 | +```html |
| 29 | +<StackLayout> |
| 30 | + <StackLayout orientation="horizontal"> |
| 31 | + <SomeItem /> |
| 32 | + <SomeItem /> |
| 33 | + </StackLayout> |
| 34 | + <StackLayout orientation="horizontal"> |
| 35 | + <!-- ... --> |
| 36 | + </StackLayout> |
| 37 | +</StackLayout> |
| 38 | +``` |
| 39 | + |
| 40 | +- Good Practice: |
| 41 | +<!-- --> |
| 42 | + |
| 43 | +```html |
| 44 | +<GridLayout rows="auto, auto" columns="auto, auto"> |
| 45 | + <SomeItem row="0" col="0" /> |
| 46 | + <SomeItem row="0" col="1" /> |
| 47 | + <!-- ... row="1" ... --> |
| 48 | +</GridLayout> |
| 49 | +``` |
| 50 | + |
| 51 | +One-offs are fine with the `<StackLayout>` approach, but building an entire app with the first approach will usually result in poor performance. |
| 52 | + |
| 53 | +::: |
0 commit comments