在网上找了挺久都没找到好用的自动换行布局,于是干脆自己写一个吧,原理其实就是计算布局大小,然后摆放的时候计算子view摆放的时候是否大于父view的最大宽度,超过了就在下面一行摆放就好了,注释就不多写了 直接上代码大家拿去用吧
class AutoWrapLayout @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val width = MeasureSpec.getSize(widthMeasureSpec)
var right: Int = 0
var height: Int = paddingTop + paddingBottom
var lineMaxHeight = 0
for (i in 0 until childCount) {
val view = getChildAt(i)
val layoutParams = view.layoutParams as FrameLayout.LayoutParams
val viewWidth = view.measuredWidth + layoutParams.leftMargin + layoutParams.rightMargin
val viewHeight = view.measuredHeight + layoutParams.topMargin + layoutParams.bottomMargin
if (right + viewWidth > width) {
height += lineMaxHeight
right = 0
lineMaxHeight = 0
}
if (right == 0) right = paddingLeft + paddingRight
right += viewWidth
if (lineMaxHeight < viewHeight) lineMaxHeight = viewHeight
if (i == childCount - 1) height += lineMaxHeight
}
setMeasuredDimension(width, height)
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
var childleft: Int = 0
var childtop: Int = paddingTop
for (i in 0 until childCount) {
val view = getChildAt(i)
val layoutParams = view.layoutParams as FrameLayout.LayoutParams
val viewWidth = view.measuredWidth + layoutParams.leftMargin + layoutParams.rightMargin
val viewHeight = view.measuredHeight + layoutParams.topMargin + layoutParams.bottomMargin
if (childleft + viewWidth > right - paddingRight) {
childtop += viewHeight
childleft = 0
}
if (childleft == 0) childleft += paddingLeft
view.layout(childleft, childtop, childleft + viewWidth, childtop + viewHeight)
childleft += viewWidth
}
}
}
|