1# 尺寸设置 2 3用于设置组件的宽高、边距。 4 5> **说明:** 6> 7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 属性 11 12 13| 名称 | 参数说明 | 描述 | 14| -------------- | ---------------------------------------- | ---------------------------------------- | 15| width | [Length](ts-types.md#length) | 设置组件自身的宽度,缺省时使用元素自身内容需要的宽度。若子组件的宽大于父组件的宽,则会画出父组件的范围。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br />从API version 10开始,该接口支持calc计算特性。 | 16| height | [Length](ts-types.md#length) | 设置组件自身的高度,缺省时使用元素自身内容需要的高度。若子组件的高大于父组件的高,则会画出父组件的范围。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br />从API version 10开始,该接口支持calc计算特性。 | 17| size | {<br/>width?: [Length](ts-types.md#length),<br/>height?: [Length](ts-types.md#length)<br/>} | 设置高宽尺寸。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br />从API version 10开始,该接口支持calc计算特性。 | 18| padding | [Padding](ts-types.md#padding) \| [Length](ts-types.md#length) | 设置内边距属性。<br/>参数为Length类型时,四个方向内边距同时生效。<br>默认值:0 <br>padding设置百分比时,上下左右内边距均以父容器的width作为基础值。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br />从API version 10开始,该接口支持calc计算特性。 | 19| margin | [Margin](ts-types.md#margin) \| [Length](ts-types.md#length) | 设置外边距属性。<br/>参数为Length类型时,四个方向外边距同时生效。<br>默认值:0 <br>margin设置百分比时,上下左右外边距均以父容器的width作为基础值。在Row、Column、Flex交叉轴上布局时,子组件交叉轴的大小与margin的和为整体。<br/>例如Column容器宽100,其中子组件宽50,margin left为10,right为20,子组件实际的水平方向offset为20。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br />从API version 10开始,该接口支持calc计算特性。 | 20| 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/>} | 设置约束尺寸,组件布局时,进行尺寸范围限制。constraintSize的优先级高于Width和Height。取值结果参考[constraintSize取值对width/height影响](ts-universal-attributes-size.md#constraintsize取值对widthheight影响)。<br>默认值:<br>{<br/>minWidth: 0,<br/>maxWidth: Infinity,<br/>minHeight: 0,<br/>maxHeight: Infinity<br/>}<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br />从API version 10开始,该接口支持calc计算特性。 | 21 22## constraintSize取值对width/height影响 23 24| 缺省值 | 结果 | 25| ---------------------------------------- | ------------------ | 26| / | max(minWidth/minHeight, min(maxWidth/maxHeight, width/height)) | 27| maxWidth/maxHeight | max(minWidth/minHeight, width/height) | 28| minWidth/minHeight | min(maxWidth/maxHeight, width/height) | 29|width/height|maxWidth/maxHeight > minWidth/minHeight时使用组件自身布局逻辑,<br/>结果在maxWidth/maxHeight与minWidth/minHeight之间。<br/> 其他情况结果为max(minWidth/minHeight, maxWidth/maxHeight)。 | 30|maxWidth/maxHeight && width/height| minWidth/minHeight | 31|minWidth/minHeight && width/height| 使用组件自身布局逻辑,最终结果不超过maxWidth/maxHeight | 32|maxWidth/maxHeight && minWidth/minHeight| width/height,根据其他布局属性可能拉伸或者压缩。 | 33maxWidth/maxHeight && minWidth/minHeight && width/height|使用父容器传递的布局限制进行布局。| 34 35## 示例 36 37```ts 38// xxx.ets 39@Entry 40@Component 41struct SizeExample { 42 build() { 43 Column({ space: 10 }) { 44 Text('margin and padding:').fontSize(12).fontColor(0xCCCCCC).width('90%') 45 Row() { 46 // 宽度80 ,高度80 ,外边距20(蓝色区域),内边距10(白色区域) 47 Row() { 48 Row().size({ width: '100%', height: '100%' }).backgroundColor(Color.Yellow) 49 } 50 .width(80) 51 .height(80) 52 .padding(10) 53 .margin(20) 54 .backgroundColor(Color.White) 55 }.backgroundColor(Color.Blue) 56 57 Text('constraintSize').fontSize(12).fontColor(0xCCCCCC).width('90%') 58 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') 59 .width('90%') 60 .constraintSize({ maxWidth: 200 }) 61 62 Text('layoutWeight').fontSize(12).fontColor(0xCCCCCC).width('90%') 63 // 父容器尺寸确定时,设置了layoutWeight的子元素在主轴布局尺寸按照权重进行分配,忽略本身尺寸设置。 64 Row() { 65 // 权重1,占主轴剩余空间1/3 66 Text('layoutWeight(1)') 67 .size({ width: '30%', height: 110 }).backgroundColor(0xFFEFD5).textAlign(TextAlign.Center) 68 .layoutWeight(1) 69 // 权重2,占主轴剩余空间2/3 70 Text('layoutWeight(2)') 71 .size({ width: '30%', height: 110 }).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center) 72 .layoutWeight(2) 73 // 未设置layoutWeight属性,组件按照自身尺寸渲染 74 Text('no layoutWeight') 75 .size({ width: '30%', height: 110 }).backgroundColor(0xD2B48C).textAlign(TextAlign.Center) 76 }.size({ width: '90%', height: 140 }).backgroundColor(0xAFEEEE) 77 // calc计算特性 78 Text('calc:').fontSize(12).fontColor(0xCCCCCC).width('90%') 79 Text('calc test').fontSize(50).fontWeight(FontWeight.Bold).backgroundColor(0xFFFAF0).textAlign(TextAlign.Center) 80 .margin('calc(25vp*2)') 81 .size({width:'calc(90%)', height:'calc(50vp + 10%)'}) 82 }.width('100%').margin({ top: 5 }) 83 } 84} 85``` 86 87 88