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

具体效果:


原理:
其实很简单,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) ; 
        }

}


代码下载地址:
 百度网盘:  http://pan.baidu.com/s/1hqH1kFU 


附件列表

Android自定义之流式布局的更多相关文章

  1. 自定义ViewGroup 流式布局

    使用 public class MainActivity extends Activity {     @Override     protected void onCreate(Bundle sav ...

  2. 28 自定义View流式布局

    流式布局每行的行高以本行中最高的元素作为高,如果一个元素放不下到一行时直接到第二行 FlowLayoutView package com.qf.sxy.customview05.widget; imp ...

  3. Android控件进阶-自定义流式布局和热门标签控件

    技术:Android+java   概述 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧,类 ...

  4. 自定义View(三)--实现一个简单地流式布局

    Android中的流式布局也就是常说的瀑布流很是常见,不仅在很多项目中都能见到,而且面试中也有很多面试官问道,那么什么是流式布局呢?简单来说就是如果当前行的剩余宽度不足以摆放下一个控件的时候,则自动将 ...

  5. Android 自动换行流式布局的RadioGroup

    效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...

  6. Android 自定义View修炼-Android中常见的热门标签的流式布局的实现

    一.概述:在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出哈) 类似的 ...

  7. 【Android - 自定义View】之自定义可滚动的流式布局

    首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 FlowLayout ,继承自ViewGroup类: (2)在这个自定义View中,用户可以放入所有继承自View类的视图,这个 ...

  8. 自定义流式布局:ViewGroup的测量与布局

    目录 1.View生命周期以及View层级 1.1.View生命周期 1.2.View层级 2.View测量与MeasureSpec类 2.1.MeasureSpec类 2.2.父View的限制 :测 ...

  9. Android流式布局实现

    查看我的所有开源项目[开源实验室] 欢迎增加我的QQ群:[201055521],本博客client下载[请点击] 摘要 新项目用到了一种全新布局----Android标签流式布局的功能,正好一直说给大 ...

随机推荐

  1. PHP脚本占用内存太多,解决方案

    Fatal Error: Allowed memory size of xxxxxx bytes exhausted 今天遇到服务器很多自动任务的php脚本占用服务内存过多,并且程序不在运行了. 解决 ...

  2. 请设计实现一个商城系统开发v2.0【代码优化】

    #!/usr/bin/env python 优化的部分:1.改用字典取键,来调用函数[原来是用if-else判断] [补充]:也可以用列表,按索引取,可以在列表最前面加一个“”任意元素,凑成一个.就和 ...

  3. PyQt 5+qtDesigner

    https://blog.csdn.net/view994/article/details/84402069 https://blog.csdn.net/yizhou2010/article/deta ...

  4. abp + angular 前端使用 hash ,登录界面不跳转问题

    abp 项目默认的路由没有使用hash,这会导致手动刷新浏览器时,页面404错误: 解决方法网上很多,就是在路由里添加一个{useHash: true},就行了. #用Hash带来的新问题# abp框 ...

  5. Windows 安装Angular CLI

    1.安装nvm npm cnpm nrm(onenote笔记上有记录) 参考:https://blog.csdn.net/tyro_java/article/details/51232458 提示:如 ...

  6. Kibana源码分析--Hapijs路由设置理解笔记

    [ES6解构赋值]:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_ ...

  7. poj1002 字典树+map+查询单词出现次数

    487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 309235   Accepted: 55223 Descr ...

  8. PIE SDK打开矢量数据

    1. 功能简介 GIS将地理空间数据表示为矢量数据和栅格数据.矢量数据模型使用点.线和多边形来表示具有清晰空间位置和边界的空间要素,如控制点.河流和宗地等,每个要素被赋予一个ID,以便与其属性相关联. ...

  9. gulp打包js

    在终端定位到你要创建目录的地方,输入 sudo mkdir js 创建文件夹,这个文件夹就是放你要压缩js文件的地方 输入 sudo vim gulpfile.js 这个js就是写gulp所有的配置信 ...

  10. 基于云计算的IaaS、PaaS、SaaS三种服务模式的区别

    Infrastructure-as-a-Service(IaaS) - 基础即设施服务 基础设施主要包括网络系统(networking).存储设备(storage).服务器(servers).虚拟化技 ...