ts-universal-attributes-layout-constraints.md 4.6 KB
Newer Older
Z
zengyawen 已提交
1
# Layout Constraints
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
Layout constraints refer to constraints on the aspect ratio and display priority of components.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
>  **NOTE**
6
>
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10 11

## Attributes

E
ester.zhou 已提交
12 13 14 15
| Name           | Type| Description                                                        |
| --------------- | -------- | ------------------------------------------------------------ |
| aspectRatio     | number   | Aspect ratio of the component, which can be obtained using the following formula: Width/Height.<br>Since API version 9, this API is supported in ArkTS widgets.|
| displayPriority | number   | Display priority for the component in the layout container. When the space of the parent container is insufficient, the component with a lower priority is hidden.<br>The digits after the decimal point are not counted in determining the display priority. That is, numbers in the [x, x + 1) range are considered to represent the same priority. For example, **1.0** and **1.9** represent the same priority.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute is valid only for the **\<Row>**, **\<Column>**, and **\<Flex>** (single-row) container components.|
Z
zengyawen 已提交
16 17 18 19


## Example

20 21
```ts
// xxx.ets
Z
zengyawen 已提交
22 23 24
@Entry
@Component
struct AspectRatioExample {
E
ester.zhou 已提交
25
  private children: string[] = ['1', '2', '3', '4', '5', '6']
Z
zengyawen 已提交
26 27

  build() {
E
ester.zhou 已提交
28
    Column({ space: 20 }) {
Z
zengyawen 已提交
29
      Text('using container: row').fontSize(14).fontColor(0xCCCCCC).width('100%')
E
ester.zhou 已提交
30
      Row({ space: 10 }) {
Z
zengyawen 已提交
31
        ForEach(this.children, (item) => {
E
ester.zhou 已提交
32
          // Component width = Component height x 1.5 = 90
Z
zengyawen 已提交
33 34 35 36 37
          Text(item)
            .backgroundColor(0xbbb2cb)
            .fontSize(20)
            .aspectRatio(1.5)
            .height(60)
E
ester.zhou 已提交
38
          // Component height = Component width/1.5 = 60/1.5 = 40
Z
zengyawen 已提交
39 40 41 42 43
          Text(item)
            .backgroundColor(0xbbb2cb)
            .fontSize(20)
            .aspectRatio(1.5)
            .width(60)
E
ester.zhou 已提交
44
        }, item => item)
Z
zengyawen 已提交
45
      }
E
ester.zhou 已提交
46
      .size({ width: "100%", height: 100 })
Z
zengyawen 已提交
47 48 49
      .backgroundColor(0xd2cab3)
      .clip(true)

E
ester.zhou 已提交
50
      // Grid child component width/height = 3/2
Z
zengyawen 已提交
51 52 53 54 55 56 57
      Text('using container: grid').fontSize(14).fontColor(0xCCCCCC).width('100%')
      Grid() {
        ForEach(this.children, (item) => {
          GridItem() {
            Text(item)
              .backgroundColor(0xbbb2cb)
              .fontSize(40)
E
ester.zhou 已提交
58
              .width('100%')
Z
zengyawen 已提交
59 60
              .aspectRatio(1.5)
          }
E
ester.zhou 已提交
61
        }, item => item)
Z
zengyawen 已提交
62 63 64 65
      }
      .columnsTemplate('1fr 1fr 1fr')
      .columnsGap(10)
      .rowsGap(10)
E
ester.zhou 已提交
66
      .size({ width: "100%", height: 165 })
Z
zengyawen 已提交
67 68 69 70 71 72
      .backgroundColor(0xd2cab3)
    }.padding(10)
  }
}
```

E
ester.zhou 已提交
73 74
**Figure 1** Portrait display

E
ester.zhou 已提交
75
![en-us_image_0000001256978379](figures/en-us_image_0000001256978379.gif)
Z
zengyawen 已提交
76

E
ester.zhou 已提交
77 78
**Figure 2** Landscape display

E
ester.zhou 已提交
79
![en-us_image_0000001212218476](figures/en-us_image_0000001212218476.gif)
Z
zengyawen 已提交
80

81
```ts
Z
zengyawen 已提交
82
class ContainerInfo {
E
ester.zhou 已提交
83 84
  label: string = '';
  size: string = '';
Z
zengyawen 已提交
85 86 87
}

class ChildInfo {
E
ester.zhou 已提交
88 89
  text: string = '';
  priority: number = 0;
Z
zengyawen 已提交
90 91 92 93 94
}

@Entry
@Component
struct DisplayPriorityExample {
E
ester.zhou 已提交
95 96 97 98 99
  // Display the container size.
  private container: ContainerInfo[] = [
    { label: 'Big container', size: '90%' },
    { label: 'Middle container', size: '50%' },
    { label: 'Small container', size: '30%' }
100
  ]
E
ester.zhou 已提交
101 102 103 104 105 106
  private children: ChildInfo[] = [
    { text: '1\n(priority:2)', priority: 2 },
    { text: '2\n(priority:1)', priority: 1 },
    { text: '3\n(priority:3)', priority: 3 },
    { text: '4\n(priority:1)', priority: 1 },
    { text: '5\n(priority:2)', priority: 2 }
107
  ]
E
ester.zhou 已提交
108
  @State currentIndex: number = 0;
Z
zengyawen 已提交
109 110

  build() {
E
ester.zhou 已提交
111 112
    Column({ space: 10 }) {
      // Switch the size of the parent container.
Z
zengyawen 已提交
113
      Button(this.container[this.currentIndex].label).backgroundColor(0x317aff)
E
ester.zhou 已提交
114 115
        .onClick(() => {
          this.currentIndex = (this.currentIndex + 1) % this.container.length;
Z
zengyawen 已提交
116
        })
E
ester.zhou 已提交
117 118 119 120
      // Set the width for the parent flex container through variables.
      Flex({ justifyContent: FlexAlign.SpaceBetween }) {
        ForEach(this.children, (item) => {
          // Bind the display priority to the child component through displayPriority.
Z
zengyawen 已提交
121 122 123 124 125 126 127
          Text(item.text)
            .width(120)
            .height(60)
            .fontSize(24)
            .textAlign(TextAlign.Center)
            .backgroundColor(0xbbb2cb)
            .displayPriority(item.priority)
E
ester.zhou 已提交
128
        }, item => item.text)
Z
zengyawen 已提交
129 130 131
      }
      .width(this.container[this.currentIndex].size)
      .backgroundColor(0xd2cab3)
E
ester.zhou 已提交
132
    }.width("100%").margin({ top: 50 })
Z
zengyawen 已提交
133 134 135 136 137
  }
}

```

E
ester.zhou 已提交
138 139
Landscape display in containers of different sizes

E
ester.zhou 已提交
140
![en-us_image_0000001212058504](figures/en-us_image_0000001212058504.gif)