流式布局,优点就是父类布局能够自己主动的推断子孩子是不是须要换行,什么时候须要换行,能够做到网页版的标签的效果。

今天就是简单的做了自己定义的流式布局。

详细效果:









原理:

事实上非常easy,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. Android流式布局实现

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

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

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

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

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

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

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

  5. 含有过滤功能的android流式布局

    FilterFlowLayout 含有过滤功能的流式布局, 參考FlowLayout 能够去除宽度不在范围(比例或真实值)内的子view 能够设置最大行数 能够加入组件间水平间距 能够加入行间距 系统 ...

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

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

  7. android -------- 流式布局,支持单选、多选等

    最近开发中有流式标签这个功能,网上学了下,来分享一下 Android 流式布局,支持单选.多选等,适合用于产品标签等. 效果图: 用法: dependencies { compile 'com.hym ...

  8. android流式布局、待办事项应用、贝塞尔曲线、MVP+Rxjava+Retrofit、艺术图片应用等源码

    Android精选源码 android模仿淘宝首页效果源码 一款艺术图片应用,采用T-MVVM打造 Android MVP + RxJava + Retrofit项目 android流式布局实现热门标 ...

  9. Android自定义之流式布局

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

随机推荐

  1. 【stl小记】list

    list相当于双向链表,所以快插快删比较方便(链式数据结构的性质),但是随机读取较慢 用一道luogu的水题做一做list,code如下 #include <cstdio> #includ ...

  2. [USACO 2018 Open Gold] Tutorial

    Link: 传送门 A: 对于每一条分割线,设本不应在其左侧的个数为$x$ 重点要发现每次一来一回的操作恰好会将一对分别应在左/右侧的一个数从右/左移过去 这样就转直接用树状数组求出最大的$x$即可 ...

  3. bzoj 2665: [cqoi2012]编号

    题目中说任意两个数至少要有3个位上数不相同,那么其实也就是从7个数中选出5个这样任意的组合全部不同,用数组f[i][j][k][l][m][n]记一下就好了,i为第几种组合,一共C(7,5)种,最后爆 ...

  4. devfs、sysfs、udev介绍

    转:http://www.360doc.com/content/11/1203/09/7378000_169310928.shtml 一.devfs linux下有专门的文件系统用来对设备进行管理,d ...

  5. Sysfs文件系统与Linux设备模型

    转:http://www.360doc.com/content/11/1218/16/1299815_173168170.shtml sysfs把连接在系统上的设备和总线组织成为一个分级的目录及文件, ...

  6. AS3.0 效率优化

    1.显示对象:1.1.静态的不需互动的图形,使用Shape对象.(eg:getSize(newShape())=236) 1.2.不需要时间轴的互动图形,使用Sprite对象.(eg:getSize( ...

  7. Controller和RestController的区别

    1. Controller, RestController的共同点 都是用来表示Spring某个类的是否可以接收HTTP请求 2.  Controller, RestController的不同点 @C ...

  8. JAVA常见算法题(二十八)

    package com.forezp.util; import java.util.Arrays; /** * 两个int数组,都是从小到大的的排列,请合并为一个新的数组,也是从小到到大的排列, * ...

  9. 逻辑回归Logistic Regression 之基础知识准备

    0. 前言   这学期 Pattern Recognition 课程的 project 之一是手写数字识别,之二是做一个网站验证码的识别(鸭梨不小哇).面包要一口一口吃,先尝试把模式识别的经典问题—— ...

  10. Codeforces Round #313 (Div. 1) Gerald&#39;s Hexagon

    http://codeforces.com/contest/559/problem/A 题目大意:按顺序给出一个各内角均为120°的六边形的六条边长,求该六边形能分解成多少个边长为1的单位三角形. 解 ...