maxWidth = placeable.width
}
placeable
}
layout(maxWidth,totalHeight){
if(layoutDirection == LayoutDirection.Ltr){
var y = 0
placeables.forEach {
it.place(0,y)
y+=it.height
}
}else{
var y = totalHeight
placeables.forEach {
y-=it.height
it.place(0,y)
}
}
}
}
}
复制代码
-
我们定义了一个CustomColumnView的可组合函数
-
定义了三个参数,layoutDirection去控制方向,modifier修饰符,内容可组合函数content。
-
内部的实现我们是通过Layout去自定义布局
-
测量每个子view的宽高 val placeable = it.measure(constraints) 自定义布局第一步需要通过父控件给的约束条件constraints跟measurable去执行measure测量。获得Placeable对象。该对象里可以获取到测量完成的每个子item的宽width跟高height。
-
接着我们根据测量好的所有的子view的宽高,可以去计算出我们自定义CustomColumnView所需要的宽高,高我们用totalHeight变量先存储,宽我们用maxWidth变