Android怎么自定义ViewGroup实现堆叠头像的点赞
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,今天小编给大家分享一下Android怎么自定义ViewGroup实现堆叠头像的点赞的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这
千家信息网最后更新 2025年11月07日Android怎么自定义ViewGroup实现堆叠头像的点赞
今天小编给大家分享一下Android怎么自定义ViewGroup实现堆叠头像的点赞的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
实现
自定义属性
| 属性名 | 说明 | 默认值 |
|---|---|---|
| vertivalSpace | 行距 | 4dp |
| pileWidth | 重叠宽度 | 10dp |
onMeasure方法,每行的宽度不再是child的宽度和了,而是要减掉重叠部分的宽度和
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); //AT_MOST int width = 0; int height = 0; int rawWidth = 0;//当前行总宽度 int rawHeight = 0;// 当前行高 int rowIndex = 0;//当前行位置 int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); if(child.getVisibility() == GONE){ if(i == count - 1){ //最后一个child height += rawHeight; width = Math.max(width, rawWidth); } continue; } //这里调用measureChildWithMargins 而不是measureChild measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; if(rawWidth + childWidth - (rowIndex > 0 ? pileWidth : 0)> widthSpecSize - getPaddingLeft() - getPaddingRight()){ //换行 width = Math.max(width, rawWidth); rawWidth = childWidth; height += rawHeight + vertivalSpace; rawHeight = childHeight; rowIndex = 0; } else { rawWidth += childWidth; if(rowIndex > 0){ rawWidth -= pileWidth; } rawHeight = Math.max(rawHeight, childHeight); } if(i == count - 1){ width = Math.max(rawWidth, width); height += rawHeight; } rowIndex++; } setMeasuredDimension( widthSpecMode == MeasureSpec.EXACTLY ? widthSpecSize : width + getPaddingLeft() + getPaddingRight(), heightSpecMode == MeasureSpec.EXACTLY ? heightSpecSize : height + getPaddingTop() + getPaddingBottom() ); }onLayout 每一行,第一个正常放,之后的重叠放
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int viewWidth = r - l; int leftOffset = getPaddingLeft(); int topOffset = getPaddingTop(); int rowMaxHeight = 0; int rowIndex = 0;//当前行位置 View childView; for( int w = 0, count = getChildCount(); w < count; w++ ){ childView = getChildAt(w); if(childView.getVisibility() == GONE) continue; MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams(); // 如果加上当前子View的宽度后超过了ViewGroup的宽度,就换行 int occupyWidth = lp.leftMargin + childView.getMeasuredWidth() + lp.rightMargin; if(leftOffset + occupyWidth + getPaddingRight() > viewWidth){ leftOffset = getPaddingLeft(); // 回到最左边 topOffset += rowMaxHeight + vertivalSpace; // 换行 rowMaxHeight = 0; rowIndex = 0; } int left = leftOffset + lp.leftMargin; int top = topOffset + lp.topMargin; int right = leftOffset+ lp.leftMargin + childView.getMeasuredWidth(); int bottom = topOffset + lp.topMargin + childView.getMeasuredHeight(); childView.layout(left, top, right, bottom); // 横向偏移 leftOffset += occupyWidth; // 试图更新本行最高View的高度 int occupyHeight = lp.topMargin + childView.getMeasuredHeight() + lp.bottomMargin; if(rowIndex != count - 1){ leftOffset -= pileWidth;//这里控制重叠位置 } rowMaxHeight = Math.max(rowMaxHeight, occupyHeight); rowIndex++; } }效果图
因为这个一般只会显示一行,所以暂时没有通过setAdapter方式去设置数据源。
以上就是"Android怎么自定义ViewGroup实现堆叠头像的点赞"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。
宽度
知识
篇文章
位置
头像
一行
内容
属性
更新
不同
很大
最高
大部分
就是
效果
效果图
数据
数据源
方式
方法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
公司如何保护云服务器安全
电子商务的网络安全分析
网络安全 可靠性
苹果登陆qq邮箱服务器没响应
物流信息技术数据库
分类数据库管理
数据库设计选课系统
服务器8m支持多大访问量
纺织技术数据库
个人软件开发哪里好找
设有关系数据库中一个表s
网络安全保卫方案
广东服务器机柜价钱多少
刚毕业当软件开发
服务器批量远程桌面
中卫软件开发销售公司
数据库中的er图用什么工
数据库语言可视化
数据库安全防护魔力象限
深圳私人承接软件开发定制
宏基笔记本电脑软件开发
计算机网络技术职称评定
外网服务器ip地址免费
墨西哥服务器可以用信用卡吗
勇帝网络技术服务工作室
2017年最新网络安全法
成考计算机网络技术学什么
数据库更换字段名
数据库中的er图用什么工
服务器无线配置要求