Android下自动折行 效果
来源:互联网 发布:医学继续教育网络 编辑:程序博客网 时间:2024/05/16 11:15
下面的代码只是一个demo,真正用到我们的项目的时候,出现了一些小的bug,后来引用一个开源项目实现了相同的效果,所以如果真的在项目中应用的话推荐使用这个开源项目:/android-flowlayout , 点击打开链接。不要重复花时间再造轮子。
那么下面的代码只是给大家一个参考,抛砖引玉。
@Deprecated
关于自动折行,网上面已经有很多实现代码了,我在之前也实现过,最近再次用上这个效果,发现之前自己写的代码很丑陋,于是进行了重构,现在把代码放出来。
import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.LinearLayout;public class FlowLinearLayout extends LinearLayout { public FlowLinearLayout(Context context) { super(context); } public FlowLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int maxWidth = MeasureSpec.getSize(widthMeasureSpec); int childCount = getChildCount(); int measuredX = 0; int measuredY = 0; int raw = 1; int maxRowItemHeight = 0; for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() == View.GONE) { return; } child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); if (height > maxRowItemHeight) { measuredY = getMeasuredY(measuredY, raw, maxRowItemHeight, height); maxRowItemHeight = height; } measuredX += width; if (measuredX > maxWidth) { measuredX = width; raw++; measuredY += height; maxRowItemHeight = 0; } } measuredY += getPaddingTop() + getPaddingBottom(); setMeasuredDimension(maxWidth, measuredY); } private int getMeasuredY(int measuredY, int raw, int maxRowItemHeight, int height) { if (raw == 1) { measuredY = height; } else { measuredY += height - maxRowItemHeight; } return measuredY; } @Override protected void onLayout(boolean changed, int l, int t, int rightBorder, int b) { int childCount = getChildCount(); int maxRowItemHeight = 0; int measuredX = getPaddingLeft(); int measuredY; int raw = 1; for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() == View.GONE) { return; } int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); if (height > maxRowItemHeight) { maxRowItemHeight = height; } measuredX += width; if (measuredX > rightBorder) { measuredX = getPaddingLeft() + width; raw++; } measuredY = getPaddingTop() + raw * maxRowItemHeight; child.layout(measuredX - width, measuredY - maxRowItemHeight, measuredX, measuredY); } }}
总共代码100行,实现了显示效果如下图
小遗憾是没有写magin,所以子元素很紧凑,大家可以自己写一个magrin参数,添加上对于的效果。
0 0
- Android下自动折行 效果
- android中Gallery画廊效果(自动滚动)
- android自定义控件自动换行效果实现
- android自定义控件自动换行效果实现
- android中实现自动输入文本效果
- android自定义控件实现自动换行效果
- Android中自动滚动播放效果
- android自定义控件自动换行效果实现
- Android ListView的自动播放效果
- android 按钮按下效果
- android Button按下效果
- IOS8下利用自动布局实现键盘的弹出效果
- Android平台下实现渐变效果
- Android自定义控件及按下效果
- Android平台下实现渐变效果
- android 按钮按下效果2
- android 按钮按下效果3
- Android的按钮按下效果
- 树莓派连接触摸屏方法(Raspberry Pi 3.5寸 B/B+/2B LCD液晶屏)
- 安卓下的多线程
- UVa11054--Wine trading in Gergovia(贪心)
- 将数据库中大量数据以JSON格式传给JQGrid MVC4+JQGrid
- hdu1011 Starship Troopers
- Android下自动折行 效果
- CF 554B Ohana Cleans Up
- iOS开发拓展篇- 即时通讯XMPP简单介绍
- 【NGUI】Unity中ScrollView3D滚动效果
- hadoop伪分布式搭建
- 接口回调
- iOS开发实用技巧篇—项目新特性页面的处理
- final方法、final变量、final类、final对象—Java
- OC字符串处理