ts-universal-attributes-size.md 5.5 KB
Newer Older
Z
zengyawen 已提交
1
# Size
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The size attributes set the width, height, and margin of a component.
Z
zengyawen 已提交
4

5 6
>  **NOTE**
>
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 9 10 11


## Attributes

Z
zengyawen 已提交
12

E
ester.zhou 已提交
13 14 15 16 17 18 19 20
| Name          | Type                                                    | Description                                                        |
| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| width          | [Length](ts-types.md#length)                                 | Width of the component. By default, the width required to fully hold the component content is used. If the width of the component is greater than that of the parent container, the range of the parent container is drawn.<br>Since API version 9, this API is supported in ArkTS widgets.|
| height         | [Length](ts-types.md#length)                                 | Height of the component. By default, the height required to fully hold the component content is used. If the height of the component is greater than that of the parent container, the range of the parent container is drawn.<br>Since API version 9, this API is supported in ArkTS widgets.|
| size           | {<br>width?: [Length](ts-types.md#length),<br>height?: [Length](ts-types.md#length)<br>} | Size of the component.<br>Since API version 9, this API is supported in ArkTS widgets.|
| padding        | [Padding](ts-types.md#padding) \| [Length](ts-types.md#length) | Padding of the component.<br>When the parameter is of the **Length** type, the four paddings take effect.<br>Default value: **0**<br>When **padding** is set to a percentage, the width of the parent container is used as the basic value.<br>Since API version 9, this API is supported in ArkTS widgets.|
| margin         | [Margin](ts-types.md#margin) \| [Length](ts-types.md#length) | Margin of the component.<br>When the parameter is of the **Length** type, the four margins take effect.<br>Default value: **0**<br>When **margin** is set to a percentage, the width of the parent container is used as the basic value.<br>Since API version 9, this API is supported in ArkTS widgets.|
| constraintSize | {<br>minWidth?: [Length](ts-types.md#length),<br>maxWidth?: [Length](ts-types.md#length),<br>minHeight?: [Length](ts-types.md#length),<br>maxHeight?: [Length](ts-types.md#length)<br>} | Constraint size of the component, which is used to limit the size range during component layout. **constraintSize** takes precedence over **width** and **height**. If the value of **minWidth** is greater than that of **maxWidth**, only the value of **minWidth** takes effect. The same rule applies to **minHeight** and **maxHeight**.<br>Default value:<br>{<br>minWidth: 0,<br>maxWidth: Infinity,<br>minHeight: 0,<br>maxHeight: Infinity<br>}<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
21
| layoutWeight   | number \| string                                   | Weight of the component during layout. When the container size is determined, the container space is allocated along the main axis among the component and sibling components based on the layout weight, and the component size setting is ignored.<br>Default value: **0**<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>** layouts.<br>The value can be a number greater than or equal to 0 or a string that can be converted to a number.|
Z
zengyawen 已提交
22 23 24


## Example
Z
zengyawen 已提交
25

26 27
```ts
// xxx.ets
Z
zengyawen 已提交
28 29 30 31 32 33 34
@Entry
@Component
struct SizeExample {
  build() {
    Column({ space: 10 }) {
      Text('margin and padding:').fontSize(12).fontColor(0xCCCCCC).width('90%')
      Row() {
E
ester.zhou 已提交
35
        // Width: 80; height: 80; margin: 20 (blue area); padding: 10 (white area)
Z
zengyawen 已提交
36
        Row() {
E
ester.zhou 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
          Row().size({ width: '100%', height: '100%' }).backgroundColor(Color.Yellow)
        }
        .width(80)
        .height(80)
        .padding(10)
        .margin(20)
        .backgroundColor(Color.White)
      }.backgroundColor(Color.Blue)

      Text('constraintSize').fontSize(12).fontColor(0xCCCCCC).width('90%')
      Text('this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text')
        .width('90%')
        .constraintSize({ maxWidth: 200 })
Z
zengyawen 已提交
50 51

      Text('layoutWeight').fontSize(12).fontColor(0xCCCCCC).width('90%')
E
ester.zhou 已提交
52
      // When the container size is determined, the component occupies the space along the main axis based on the layout weight, and the component size setting is ignored.
Z
zengyawen 已提交
53
      Row() {
E
ester.zhou 已提交
54
        // Weight 1: The component occupies 1/3 of the remaining space along the main axis.
Z
zengyawen 已提交
55 56 57
        Text('layoutWeight(1)')
          .size({ width: '30%', height: 110 }).backgroundColor(0xFFEFD5).textAlign(TextAlign.Center)
          .layoutWeight(1)
E
ester.zhou 已提交
58
        // Weight 2: The component occupies 2/3 of the remaining space along the main axis.
Z
zengyawen 已提交
59 60 61
        Text('layoutWeight(2)')
          .size({ width: '30%', height: 110 }).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
          .layoutWeight(2)
E
ester.zhou 已提交
62
        // If layoutWeight is not set, the component is rendered based on its own size setting.
Z
zengyawen 已提交
63 64 65 66
        Text('no layoutWeight')
          .size({ width: '30%', height: 110 }).backgroundColor(0xD2B48C).textAlign(TextAlign.Center)
      }.size({ width: '90%', height: 140 }).backgroundColor(0xAFEEEE)
    }.width('100%').margin({ top: 5 })
E
ester.zhou 已提交
67 68
  }
}
Z
zengyawen 已提交
69 70
```

E
ester.zhou 已提交
71
![size](figures/size.png)