作者:XINHAO_HAN
Android中系统本身就没有给我自带的一种布局流式布局,如果要写这种效果,我们就要用RelativeLayout来实现,或者用其他布局来实现,但问题是,都不是怎么好用,又麻烦,又不省事所以,还不如我们自己动手写一个
代码十分简单,总共只有100行左右-只是简单实现,如果要在项目当中使用的哥们,需要稍微增加修改.
Warning:目前布局当中必须要有至少一个子布局
效果图:
![5337239-c4d0c76fc18ab327.gif](https://i-blog.csdnimg.cn/blog_migrate/7dd714dedf0384270abb701244f930b6.webp?x-image-process=image/format,png)
1521700893541mzflow.gif
代码:
package com.example.xinhao_han.xhflowlayout
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.ViewGroup
/**
* Created by Administrator on 2017/6/6.
*/
/*
*
* 流失布局,XINHAO_HAN
* */
class FlowLayout : ViewGroup {
var conutSize: Int = 0
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
//测量位置
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
/*
*
* 表示当前没有孩子
* */
if (childCount == 0) {
return
}
//记录当前所有孩子的宽度
var childWei: Int = 0
//记录当前多有孩子的高度
var childHei: Int = 0
//记录多少行
var cout: Int = 0
Log.e("屏幕宽度", "" + UIUtils.getWeiSize())
for (i in 0..childCount - 1) {
val childAt = getChildAt(i)
val widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
val heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
childAt.measure(widthMeasureSpec, heightMeasureSpec)
//相加当前孩子的宽高度
Log.e("测量结果", "" + childWei)
if (childWei < UIUtils.getWeiSize()) {
//一行大小正好方的下,放不下就放到下一行,没毛病
if (i == 0)
childAt.layout(0, (cout * childAt.measuredHeight), (childWei + childAt.measuredWidth), (cout * childAt.measuredHeight) + childAt.measuredHeight)
else {
if (childWei + childAt.measuredWidth > UIUtils.getWeiSize()) {
//表示本行已无力吐槽,让给下一行
cout++
//记得下一行位置记录从0开始
childWei = 0
childAt.layout(childWei, (cout * childAt.measuredHeight), (childWei + childAt.measuredWidth), (cout * childAt.measuredHeight) + childAt.measuredHeight)
} else {
childAt.layout(childWei, (cout * childAt.measuredHeight), (childWei + childAt.measuredWidth), (cout * childAt.measuredHeight) + childAt.measuredHeight)
}
}
childWei += childAt.measuredWidth
} else {
//不能放下一行,需要换行
//记录行数
cout++
//重新记录行数
childWei = 0
}
}
conutSize = cout
}
//测量大小,根据当前孩子View来决定自己的大小
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
/*
*
* 表示当前没有孩子
* */
if (childCount == 0) {
return
}
var cout: Int = 0
//记录当前所有孩子的宽度
var childWei: Int = 0
//记录当前多有孩子的高度
var childHei: Int = 0
for (i in 0..childCount - 1) {
val childAt = getChildAt(i)
childAt.measure(widthMeasureSpec, heightMeasureSpec)
//相加当前孩子的宽高度
childWei += childAt.measuredWidth
childHei += childAt.measuredHeight
}
if (childWei > UIUtils.getWeiSize()) {
childWei = UIUtils.getWeiSize()
} else {
}
super.onMeasure(MeasureSpec.makeMeasureSpec(childWei, MeasureSpec.EXACTLY), heightMeasureSpec)
}
}
Demo下载(github):https://github.com/hanxinhao000/FlowLayout