长沙做网站微联讯点很好,室内设计师常去的网站,用模板做网站教程,变更股东怎样在工商网站做公示onMeasureSize和PlaceChildren onMeasureSize和PlaceChildren 说明官方使用示例参考资料 onMeasureSize和PlaceChildren 说明
在Android开发中View的测量onMeasure和布局onLayout是自定义组件必备的两个方法#xff0c;HarmonyOS对自定义布局也提供了两个方法#xff1a; on… onMeasureSize和PlaceChildren onMeasureSize和PlaceChildren 说明官方使用示例参考资料 onMeasureSize和PlaceChildren 说明
在Android开发中View的测量onMeasure和布局onLayout是自定义组件必备的两个方法HarmonyOS对自定义布局也提供了两个方法 onMeasureSize组件每次布局时触发计算子组件的尺寸其执行时间先于onPlaceChildren。注意 不允许在onMeasureSize函数中改变状态变量。
onPlaceChildren组件每次布局时触发设置子组件的起始位置。 两个方法的参数详细含义参考自定义组件的自定义布局
官方使用示例
以下示例中Index页面包含一个实现了自定义布局的自定义组件且对应自定义组件的子组件通过index页面内的builder方式传入。
而在自定义组件中调用了onMeasureSize和onPlaceChildren设置子组件大小和放置位置。例如在本示例中在onMeasureSize中初始化组件大小size100后续的每一个子组件size会加上上一个子组件大小的一半实现组件大小递增的效果。而在onPlaceChildren中定义startPos300设置每一个子组件的位置为startPos减去子组件自身的高度所有子组件右下角一致在顶点位置(300,300)实现一个从右下角开始展示组件的类Stack组件。
// xxx.ets
Entry
Component
struct Index {build() {Column() {CustomLayout({ builder: ColumnChildren })}}
}// 通过builder的方式传递多个组件作为自定义组件的一级子组件即不包含容器组件如Column
Builder
function ColumnChildren() {ForEach([1, 2, 3], (index: number) { // 暂不支持lazyForEach的写法Text(S index).fontSize(30).width(100).height(100).borderWidth(2).offset({ x: 10, y: 20 })})
}Component
struct CustomLayout {BuilderdoNothingBuilder() {};BuilderParam builder: () void this.doNothingBuilder;State startSize: number 100;result: SizeResult {width: 0,height: 0};// 第一步计算各子组件的大小onMeasureSize(selfLayoutInfo: GeometryInfo, children: ArrayMeasurable, constraint: ConstraintSizeOptions) {let size 100;children.forEach((child) {let result: MeasureResult child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size })size result.width / 2;})this.result.width 100;this.result.height 400;return this.result;}// 第二步放置各子组件的位置onPlaceChildren(selfLayoutInfo: GeometryInfo, children: ArrayLayoutable, constraint: ConstraintSizeOptions) {let startPos 300;children.forEach((child) {let pos startPos - child.measureResult.height;child.layout({ x: pos, y: pos })})}build() {this.builder();}
}运行效果
参考资料
ConstraintSizeOptions Measurable10 自定义组件的自定义布局 自定义组件的自定义布局2 文章转载自: http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.lwdzt.cn.gov.cn.lwdzt.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.mxhys.cn.gov.cn.mxhys.cn http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.plfy.cn.gov.cn.plfy.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.jgykx.cn.gov.cn.jgykx.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com