废话少说 , 先展示一下文本瀑布流的效果 :

android中Recyclerview瀑布流 android 瀑布流_Line

自定义瀑布流控件:

package com.lyx.flowlayoutdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**强大的流式布局*/

public class FlowLayout extends ViewGroup{

    private int horizontalSpacing = 15;//水平间距
    private int verticalSpacing = 15;//行与行之间的垂直间距

    //用来存放所有的Line对象
    private ArrayList<Line> lineList = new ArrayList<Line>();
    public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlowLayout(Context context) {
        super(context);
    }
    /**
     * 设置水平间距
     * @param horizontalSpacing
     */
    public void setHorizontalSpacing(int horizontalSpacing){
        this.horizontalSpacing = horizontalSpacing;
    }

    /**
     * 设置垂直间距
     * @param verticalSpacing
     */
    public void setVerticalSpacing(int verticalSpacing){
        this.verticalSpacing = verticalSpacing;
    }

    /**
     * 分行:遍历所有的子View,判断哪几个子View在同一行(排座位表)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        lineList.clear();

        //1.获取FlowLayout的宽度
        int width = MeasureSpec.getSize(widthMeasureSpec);
        //2.获取用于实际比较的宽度,就是除去2边的padding的宽度
        int noPaddingWidth = width-getPaddingLeft()-getPaddingRight();

        //3.遍历所有的子View,拿子View的宽和noPaddingWidth进行比较
        Line line = new Line();//准备Line对象
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            childView.measure(0, 0);//保证能够获取到宽高

            //4.如果当前line中木有子View,则不用比较直接放入line中,因为要保证每行至少有一个子View;
            if(line.getViewList().size()==0){
                line.addLineView(childView);//直接存入
            }else if(line.getLineWidth()+horizontalSpacing+childView.getMeasuredWidth()>noPaddingWidth){
                //5.如果当前line的宽+水平间距+子View的宽大于noPaddingWidth,则child需要换行
                //需要先存放好之前的line对象,否则会造成丢失
                lineList.add(line);

                line = new Line();//创建新的Line,
                line.addLineView(childView);//将当前child放入新的行中
            }else {
                //6.说明当前child应该放入当前Line中
                line.addLineView(childView);
            }

            //7.如果当前child是最后的子View,那么需要保存最后的line对象
            if(i==(getChildCount()-1)){
                lineList.add(line);//保存最后的Line
            }
        }

        //for循环结束了,lineList存放了所有的Line,而每个Line又记录了自己行所有的VIew;
        //计算FLowLayout需要的高度
        int height = getPaddingTop()+getPaddingTop();//先计算上下的padding值
        for (int i = 0; i < lineList.size(); i++) {
            height += lineList.get(i).getLineHeight();//再加上所有行的高度
        }
        height += (lineList.size()-1)*verticalSpacing;//最后加上所有的行间距

        //设置当前控件的宽高,或者向父VIew申请宽高
        setMeasuredDimension(width, height);
    }
    /**
     * 去摆放所有的子View,让每个人真正的坐到自己的位置上
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        for (int i = 0; i < lineList.size(); i++) {
            Line line = lineList.get(i);//获取Line对象

            //从第二行开始,每行的top总是比上一行的top多一个行高和垂直间距
            if(i>0){
                paddingTop += verticalSpacing+lineList.get(i-1).getLineHeight();
            }

            ArrayList<View> viewList = line.getViewList();//获取line的view的集合

            //1.获取每行的留白的宽度
            int remainSpacing = getLineRemainSpacing(line);
            //2.计算每个view平均得到的值
            float perSpacing = remainSpacing/viewList.size();

            for (int j = 0; j < viewList.size(); j++) {
                View childView = viewList.get(j);
                //3.将得到的perSpacing增加到view的宽度上面
                int widthSpec = MeasureSpec.makeMeasureSpec((int) (childView.getMeasuredWidth()+perSpacing),MeasureSpec.EXACTLY);
                childView.measure(widthSpec,0);

                if(j==0){
                    //如果是每行的第一行,name直接靠左边摆放
                    childView.layout(paddingLeft,paddingTop,paddingLeft+childView.getMeasuredWidth(),
                            paddingTop+childView.getMeasuredHeight());
                }else {
                    //如果不是第一个,需要参考前一个view的right
                    View preView = viewList.get(j-1);
                    //当前view的left是前一个view的right+水平间距
                    int left = preView.getRight()+horizontalSpacing;
                    childView.layout(left, preView.getTop(),left+childView.getMeasuredWidth(),preView.getBottom());
                }
            }
        }
    }
    /**
     * 获取指定line的留白
     * @param line
     * @return
     */
    private int getLineRemainSpacing(Line line){
        return getMeasuredWidth()-getPaddingLeft()-getPaddingRight()-line.getLineWidth();
    }

    /**
     * 封装每行的数据,包括所有的子View,行的宽高
     * @author Administrator
     *
     */
    class Line{
        private ArrayList<View> viewList;//用来存放当前行所有的子View
        private int width;//表示所有子View的宽+水平间距
        private int height;//行的高度

        public Line(){
            viewList = new ArrayList<View>();
        }

        /**
         * 记录子VIew
         * @param child
         */
        public void addLineView(View child){
            if(!viewList.contains(child)){
                viewList.add(child);

                //1.更新Line的width
                if(viewList.size()==1){
                    //说明添加的是第一个子View,那么line的宽就是子view的宽度
                    width = child.getMeasuredWidth();
                }else {
                    //如果添加的不是第一个子View,那么应该加等于水平间距和子VIew的宽度
                    width += child.getMeasuredWidth()+horizontalSpacing;
                }
                //2.更新line的height
                height = Math.max(height,child.getMeasuredHeight());
            }
        }

        /**
         * 获取当前行的宽度
         * @return
         */
        public int getLineWidth(){
            return width;
        }
        /**
         * 获取当前行的高度
         * @return
         */
        public int getLineHeight(){
            return height;
        }
        /**
         * 获取当前行的所有的子View
         * @return
         */
        public ArrayList<View> getViewList(){
            return viewList;
        }
    }

}

使用代码:

package com.lyx.flowlayoutdemo;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private FlowLayout flowLayout;
    ArrayList<String> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flowLayout = (FlowLayout) findViewById(R.id.fly);

        initData();

        for (String data : datas) {
            final TextView textView = new TextView(MainActivity.this);
            textView.setText(data);

            //背景图片
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);
            int dp5 = Utils.getDimens(MainActivity.this, R.dimen.dp5);
            gradientDrawable.setCornerRadius(dp5);
            gradientDrawable.setColor(
                    Color.rgb(Utils.createRandomColor(), Utils.createRandomColor(), Utils.createRandomColor()));

            GradientDrawable gradientDrawable2 = new GradientDrawable();
            gradientDrawable2.setShape(GradientDrawable.RECTANGLE);

            gradientDrawable2.setCornerRadius(dp5);
            gradientDrawable2.setColor(
                    Color.rgb(Utils.createRandomColor(), Utils.createRandomColor(), Utils.createRandomColor()));

            textView.setTextColor(Color.WHITE);
            textView.setTextSize(16);
            textView.setPadding(10, 5, 10, 5);
            textView.setGravity(Gravity.CENTER);

            //设置点击效果
            StateListDrawable stateListDrawable = new StateListDrawable();
            stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, gradientDrawable);
            stateListDrawable.addState(new int[]{}, gradientDrawable2);
            textView.setBackgroundDrawable(stateListDrawable);


            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utils.showToast(MainActivity.this, textView.getText().toString());
                }
            });

            flowLayout.addView(textView);
        }
    }

    private void initData() {
        for (int i = 0; i < 20; i++) {
            datas.add("QQ");
            datas.add("暴风影音");
            datas.add("王者农药");
            datas.add("名字非常的长,特别的长");
            datas.add("携程");
            datas.add("微信");
        }
    }


}

布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:scrollbars="none"
    tools:context="com.lyx.flowlayoutdemo.MainActivity">


    <com.lyx.flowlayoutdemo.FlowLayout
        android:id="@+id/fly"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


</ScrollView>

工具类:

package com.lyx.flowlayoutdemo;

import android.content.Context;
import android.util.DisplayMetrics;
import android.widget.Toast;

import java.util.Random;

/**
 * Created by liyongxiang on 2017/8/12.
 */

public class Utils {

    private static Toast toast;

    /**
     * 拿到一个随机颜色
     */
    public static int createRandomColor() {
        Random random = new Random();
        return random.nextInt(180);

    }

    //在屏幕适配时候使用,让代码中使用dip属性
    public static int getDimens(Context context,int px) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return dp;
    }


    public static void showToast(Context context, String text) {
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);//如果不为空,则直接改变当前toast的文本
        }
        toast.show();
    }
}