流式布局,好处就是父类布局可以自动的判断子孩子是不是需要换行,什么时候需要换行,可以做到网页版的标签的效果。今天就是简单的做了自定义的流式布局。
具体效果:

原理:
其实很简单,Measure Layout。只需要这两个步骤就可以搞定了。完全的手动去Measure Layout。
我们看一下代码。
解释就在代码里面做注释了,因为使用为知笔记写的博客,格式不符合代码格式。大家可以看具体的源码。最后又源码下载地址。
1.Measure 测量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int lineHeight = 0 ;
int lineWidth = 0 ;
int width = 0 ;
int height = 0 ;
int childCount = getChildCount();
Log.i("Test", getPaddingLeft() + "==right=" +getPaddingRight());
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
int childWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin ;
int childHeight = childView.getMeasuredHeight() + params.topMargin + params.bottomMargin ;
if ((lineWidth + childWidth ) > widthSize - getPaddingLeft() - getPaddingRight() ) {
width = Math.max(width, lineWidth);
lineWidth = childWidth ;
height += lineHeight ;
lineHeight = childHeight;
}else {
lineWidth += childWidth ;
lineHeight = Math.max(lineHeight, childHeight);
}
if (i == childCount-1) {
width = Math.max(width, lineWidth);
height += lineHeight ;
}
}
height += getPaddingTop() + getPaddingBottom() ;
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY?widthSize:width,
heightMode == MeasureSpec.EXACTLY?heightSize:height);
}
2.onLayout 布局
@Override
protected void onLayout(boolean a, int l, int t, int r, int b) {
childViewList.clear();
int childCount = getChildCount() ;
int width = getWidth();
int lineWidth = 0 ;
int lineHeight = 0 ;
List<View> lineViews = new ArrayList<View>();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
int childWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin ;
int childHeight = childView.getMeasuredHeight() + params.topMargin + params.bottomMargin ;
if (lineWidth + childWidth > width - getPaddingLeft() - getPaddingRight()) {
childViewList.add(lineViews);
lineViews = new ArrayList<View>();
if (i == 0 ) {
lineHeight += getPaddingTop() ;
}else if (i== childCount - 1) {
lineHeight += getPaddingBottom() ;
}
this.lineHeight.add(lineHeight);
lineHeight = 0 ;
lineWidth = 0 ;
}
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight) ;
lineViews.add(childView);
}
childViewList.add(lineViews);
this.lineHeight.add(lineHeight);
int left = getPaddingLeft() ;
int top = getPaddingTop();
for (int i = 0; i < childViewList.size(); i++) {
lineViews = childViewList.get(i);
for (int j = 0; j < lineViews.size(); j++) {
View childView = lineViews.get(j);
MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
int lc = left + params.leftMargin ;
int tc = top + params.topMargin ;
int rc = lc + childView.getMeasuredWidth() ;
int bc = tc + childView.getMeasuredHeight() ;
childView.layout(lc,tc,rc,bc);
left += params.leftMargin + childView.getMeasuredWidth() + params.rightMargin ;
}
left = getPaddingLeft() ;
top += this.lineHeight.get(i) ;
}
}
代码下载地址:
- 自定义ViewGroup 流式布局
使用 public class MainActivity extends Activity { @Override protected void onCreate(Bundle sav ...
- 28 自定义View流式布局
流式布局每行的行高以本行中最高的元素作为高,如果一个元素放不下到一行时直接到第二行 FlowLayoutView package com.qf.sxy.customview05.widget; imp ...
- Android控件进阶-自定义流式布局和热门标签控件
技术:Android+java 概述 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧,类 ...
- 自定义View(三)--实现一个简单地流式布局
Android中的流式布局也就是常说的瀑布流很是常见,不仅在很多项目中都能见到,而且面试中也有很多面试官问道,那么什么是流式布局呢?简单来说就是如果当前行的剩余宽度不足以摆放下一个控件的时候,则自动将 ...
- Android 自动换行流式布局的RadioGroup
效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...
- Android 自定义View修炼-Android中常见的热门标签的流式布局的实现
一.概述:在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出哈) 类似的 ...
- 【Android - 自定义View】之自定义可滚动的流式布局
首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 FlowLayout ,继承自ViewGroup类: (2)在这个自定义View中,用户可以放入所有继承自View类的视图,这个 ...
- 自定义流式布局:ViewGroup的测量与布局
目录 1.View生命周期以及View层级 1.1.View生命周期 1.2.View层级 2.View测量与MeasureSpec类 2.1.MeasureSpec类 2.2.父View的限制 :测 ...
- Android流式布局实现
查看我的所有开源项目[开源实验室] 欢迎增加我的QQ群:[201055521],本博客client下载[请点击] 摘要 新项目用到了一种全新布局----Android标签流式布局的功能,正好一直说给大 ...
随机推荐
- audiosprite的使用
github地址: https://github.com/tonistiigi/audiosprite 使用方法: http://www.jikexueyuan.com/course/2510_2.h ...
- day--38 mysql表的完整性约束总结
表的简单查询参考:https://www.cnblogs.com/clschao/articles/9995531.html 一:单表的查询: 查询数据的本质:mysql会去本地的硬盘上面找到对应的文 ...
- dapper源码(.net 4.0)
http://blog.csdn.net/ahshow/article/details/42773929
- C#生成二維碼(ThoughtWorks.QRCode)
本人使用的是ThoughtWorks.QRCode.dll,在網上可以下載,但要注意dll文件的完整性和準確性,本人之前下載的dll就是不正確導致調試時出現錯誤. 以下為cs文件代碼: using S ...
- RabbitMQ原理——exchange、route、queue的关系
从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件 从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...
- TOJ 4393 Game
描述 Bob always plays game with Alice.Today,they are playing a game on a tree.Alice has m1 stones,Bob ...
- 移动开发:Android官方提供的支持不同屏幕大小的全部方法
转载请注明出处:http://blog.csdn.net/sinyu890807/article/details/8830286 原文地址为:http://developer.android.com/ ...
- 《腾讯游戏人生》微信小程序开发总结
为打通游戏人生擂台赛与线下商家的O2O衔接,同时响应时下日臻火热的微信小程序,项目团队决定也开发一款针对性的微信小程序,以此方便商家在我们平台入驻并进行擂台赛事的创建和奖励的核销,进一步推广擂台赛的玩 ...
- 用jquery来实现正反选选择框checkbox的小示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- bzoj 5302: [Haoi2018]奇怪的背包
Description Solution 首先 \(v_1,v_2,v_3...v_n,P\) 能够构成的最小数是 \(gcd(P,v_1,v_2,v_3...v_n)\) 然后 \(gcd(P,v_ ...