1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16@Entry 17@Component 18struct Index { 19 build() { 20 Column() { 21 CustomLayout1({ builder: ColumnChildren }) 22 CustomLayout2({ builder: ColumnChildren }) 23 CustomLayout3({ builder: ColumnChildren }) 24 } 25 } 26} 27 28@Builder 29function ColumnChildren() { 30 ForEach([1, 2, 3], (index: number) => { //暂不支持lazyForEach的写法 31 Text('S' + index) 32 .fontSize(30) 33 .width(100) 34 .height(100) 35 .borderWidth(2) 36 .offset({ x: 10, y: 20 }) 37 }) 38} 39 40@Component 41struct CustomLayout1 { 42 @Builder 43 doNothingBuilder() { 44 }; 45 46 @BuilderParam builder: () => void = this.doNothingBuilder; 47 @State startSize: number = 100; 48 result: SizeResult = { 49 width: 0, 50 height: 0 51 }; 52 53 onPlaceChildren(selfLayoutInfo: GeometryInfo, children: Array<Layoutable>, constraint: ConstraintSizeOptions) { 54 let startPos = 300; 55 children.forEach((child) => { 56 let pos = startPos - child.measureResult.height; 57 child.layout({ x: pos, y: pos }) 58 }) 59 } 60 61 build() { 62 this.builder() 63 } 64} 65 66@Component 67struct CustomLayout2 { 68 @Builder 69 doNothingBuilder() { 70 }; 71 72 @BuilderParam builder: () => void = this.doNothingBuilder; 73 @State startSize: number = 100; 74 result: SizeResult = { 75 width: 0, 76 height: 0 77 }; 78 79 onMeasureSize(selfLayoutInfo: GeometryInfo, children: Array<Measurable>, constraint: ConstraintSizeOptions) { 80 let size = 100; 81 children.forEach((child) => { 82 let result: MeasureResult = child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size }) 83 size += result.width / 2 84 ; 85 }) 86 this.result.width = 100; 87 this.result.height = 400; 88 return this.result; 89 } 90 91 build() { 92 this.builder() 93 } 94} 95 96@Component 97struct CustomLayout3 { 98 @Builder 99 doNothingBuilder() { 100 }; 101 102 @BuilderParam builder: () => void = this.doNothingBuilder; 103 @State startSize: number = 100; 104 result: SizeResult = { 105 width: 0, 106 height: 0 107 }; 108 109 onMeasureSize(selfLayoutInfo: GeometryInfo, children: Array<Measurable>, constraint: ConstraintSizeOptions) { 110 let size = 100; 111 children.forEach((child) => { 112 let result: MeasureResult = child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size }) 113 size += result.width / 2; 114 }) 115 this.result.width = 100; 116 this.result.height = 400; 117 return this.result; 118 } 119 120 onPlaceChildren(selfLayoutInfo: GeometryInfo, children: Array<Layoutable>, constraint: ConstraintSizeOptions) { 121 let startPos = 300; 122 children.forEach((child) => { 123 let pos = startPos - child.measureResult.height; 124 child.layout({ x: pos, y: pos }) 125 }) 126 } 127 128 build() { 129 this.builder() 130 } 131} 132 133@Component 134struct CustomLayout4 { 135 build() { 136 137 } 138}