自定义view--流式布局


package example.com.niuyuejiao;

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

import java.util.ArrayList;
import java.util.List;

/**
 * author:Created by niuyuejiao on 2018/4/16.
 */

public class MyView extends ViewGroup {

    //每行的高度
    private List<Integer> mLineHeight = new ArrayList<>();
    //储存所有的子view
    private List<List<View>> mAllChildViews = new ArrayList<>();

    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        //测量宽度
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        //测量高度
        int sizeheight = MeasureSpec.getSize(heightMeasureSpec);
        int modeheight = MeasureSpec.getMode(heightMeasureSpec);
        //如果宽高为wrap_content的情况
        int width = 0;
        int height = 0;
        //记录每一行的宽高
        int lineWidth = 0;
        int lineHeight = 0;
        //获取一个view的个数
        int childCount = getChildCount();
        //循环获得每个view的数值
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            //测量子View的宽高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            //View 占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            //View占据的高
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            //换行的时候
            if (lineWidth + childWidth > sizeWidth) {
                //对比得到最大的宽度
                width = Math.max(width, lineWidth);
                //重置lineWidth
                lineWidth = childWidth;
                //记录行高
                height += lineHeight;
                lineHeight = childHeight;
            } else {
                //叠加行宽
                lineWidth += childWidth;
                //得到最大行高
                lineHeight = Math.max(lineHeight, childHeight);
            }
            //处理最后一个子View
            if (i == childCount - 1) {
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        //wrap_content
        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width, modeheight == MeasureSpec.EXACTLY ? sizeheight : height);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    //设置布局
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        mAllChildViews.clear();
        mLineHeight.clear();

        //获得当前ViewGroup的宽度
        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;
        // 记录当前行的View
        List<View> lineViews = new ArrayList<View>();
        int childCount = getChildCount();
        for (int f = 0; f < childCount; f++) {
            View child = getChildAt(f);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            //如果需要换行
            if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width) {
                //记录LineHeight
                mLineHeight.add(lineHeight);
                //记录当前的Views
                mAllChildViews.add(lineViews);
                //重置行高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                //重置view的集合
                lineViews = new ArrayList();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
            lineViews.add(child);
        }
        //处理最后一行
        mLineHeight.add(lineHeight);
        mAllChildViews.add(lineViews);

        //设置子View的位置
        int left = 20;
        int top = 100;
        //获取行数
        int lineCount = mAllChildViews.size();
        for (int d = 0; d < lineCount; d++) {
            //当前行的Views和高度
            lineViews = mAllChildViews.get(d);
            lineHeight = mLineHeight.get(d);

            for (int j = 0; j < lineViews.size(); j++) {
                View child = lineViews.get(j);
                //判断是否显示
                if (child.getVisibility() == View.GONE) {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int cLeft = left + lp.leftMargin;
                int cTop = top + lp.topMargin;
                int cRight = cLeft + child.getMeasuredWidth();
                int cBottom = cTop + child.getMeasuredHeight();
                //进行子View进行布局
                child.layout(cLeft, cTop, cRight, cBottom);
                left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = 20;
            top += lineHeight;
        }
    }

    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub

        return new MarginLayoutParams(getContext(), attrs);
    }

}
ackage example.com.niuyuejiao;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

    private String[] datas = {"笔记本", "空气净化器", "安卓手机", "超级大号圆珠笔", "空气滤芯", "超级大号钢笔"};
    private Button clear;
    private MyView main_view;
    private EditText editText;
    private ImageView add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        main_view = findViewById(R.id.main_view);
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 5;
        lp.rightMargin = 5;
        lp.topMargin = 5;
        lp.bottomMargin = 5;
        for (int i = 0; i < datas.length; i++) {
            TextView textView = new TextView(MainActivity.this);
            textView.setText(datas[i]);
            textView.setTextColor(Color.BLUE);
            textView.setTextSize(18);
            textView.setBackgroundResource(R.drawable.seleter);
            main_view.addView(textView, lp);
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="example.com.niuyuejiao.MainActivity">



        <example.com.niuyuejiao.MyView
            android:id="@+id/main_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </example.com.niuyuejiao.MyView>
  
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/niu_yue_jiao/article/details/79974451