
Android - 流式布局(代码)
发布日期:2021-05-08 16:16:01
浏览次数:15
分类:精选文章
本文共 3872 字,大约阅读时间需要 12 分钟。
流式布局的实现(Kotlin语言)
import android.content.Contextimport android.util.AttributeSetimport android.util.Logimport android.view.Viewimport android.view.ViewGroupimport androidx.core.view.isEmptyclass FlowLayout : ViewGroup { constructor(context: Context): super(context) {} constructor(context: Context, attrs: AttributeSet?): super(context, attrs) {} private val space = 30 var allViews: MutableList= mutableListOf() var everyLineViews: MutableList = mutableListOf() private var allLineHeightList: MutableList = mutableListOf() override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { var maxWidth = 0 var parentHeight = 0 var parentWidth = 0 var currentWidth = space var currentHeight = space var totalHeight = 0 allViews.clear() everyLineViews.clear() super.onMeasure(widthMeasureSpec, heightMeasureSpec) val parentMaxWidth = MeasureSpec.getSize(widthMeasureSpec) Log.v("zj", "父容器最大宽度:$parentMaxWidth") for (i in 0 until childCount) { val child = getChildAt(i) val lp = child.layoutParams val childWidthSpec = getChildMeasureSpec( widthMeasureSpec, 2 * space, lp.width ) val childHeightSpec = getChildMeasureSpec( heightMeasureSpec, 2 * space, lp.height ) child.measure(childWidthSpec, childHeightSpec) if (currentWidth + space + child.measuredWidth <= parentMaxWidth) { currentWidth += child.measuredWidth + space currentHeight = Math.max(currentHeight, child.measuredHeight + space) } else { allViews.add(everyLineViews) everyLineViews = mutableListOf() totalHeight += currentHeight allLineHeightList.add(currentHeight) maxWidth = Math.max(maxWidth, currentWidth) currentHeight = child.measuredHeight + space currentWidth = child.measuredWidth + space } everyLineViews.add(child) } if (!everyLineViews.isEmpty()) { currentWidth = 0 currentHeight = 0 for (i in 0 until everyLineViews.size) { val child = everyLineViews[i] currentWidth += child.measuredWidth + space currentHeight = Math.max(currentHeight, child.measuredHeight + space) } maxWidth = Math.max(maxWidth, currentWidth) totalHeight += currentHeight allLineHeightList.add(currentHeight) allViews.add(everyLineViews) } parentHeight = totalHeight + space parentWidth = maxWidth + space setMeasuredDimension(parentWidth, parentHeight) } override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { var left = space var top = space var right = 0 var bottom = 0 for (i in 0 until allViews.size) { for (j in 0 until allViews[i].size) { val child = allViews[i][j] right = left + child.measuredWidth bottom = top + child.measuredHeight child.layout(left, top, right, bottom) left += child.measuredWidth + space } top += allLineHeightList[i] left = space } }} 代码解释
该代码实现了一个自定义的流式布局View类,主要功能包括:
- 定义控件之间的间隔(space)
- 保存所有子控件的布局信息
- 按行布局子控件,自动换行
- 计算每行的高度和总体的布局参数
代码中的关键逻辑包括:
- 测量子控件的宽度和高度
- 判断是否需要换行
- 记录每行的布局信息
- 设置父容器的最终尺寸
注:代码中使用了测量布局(Layout Measurement)和布局(Layout)过程的标准方法,确保了布局的准确性和兼容性。
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年05月13日 17时48分36秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Laravel 深入理解路由和URL生成
2025-04-04
laravel 生命周期与框架精髓
2025-04-04
laravel 表单验证
2025-04-04
laravel 调试sql
2025-04-04
laravel 路由缓存
2025-04-04
Laravel 连接(Join)
2025-04-04
laravel 通过令牌获取用户ID
2025-04-04
laravel 验证机制validation
2025-04-04
Laravel5 容器自动加载依赖的原理
2025-04-04
laravel5.5 __Resource路由__RESTFul风格控制器
2025-04-04
Laravel5.5 集成 mPDF
2025-04-04
laravel5.5中添加对分页样式的修改上一页和下一页
2025-04-04
laravel5.5之模型操作数据库 — Eloquent ORM(实践)
2025-04-04
Laravel5.5开发规范 [ 个人总结 ]
2025-04-04
laravel5.5数据库迁移入门实践
2025-04-04
Laravel5.5添加新路由文件并制定规则
2025-04-04
Laravel5.5集成七牛云上传、管理(删除、查询)
2025-04-04
Laravel5.5集成极光推送_解决推送失败重推问题
2025-04-04