onlayout 换行 onMeasure 计算宽高 的添加流式布局
package mlb.bawei.com.d05_test01.View;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.List;
/**
* @author
* @date 2018/11/30
*/
public class InsertView extends LinearLayout {
private int mMaxChildHeight;
/**
* 每一个孩子的左右的间距
* 20是默认值,单位是px
*/
private int mHSpace = 20;
/**
* 每一行的上下的间距
* 20是默认值,单位是px
*/
private int mVSpace = 20;
public InsertView(Context context) {
super(context);
}
public InsertView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 拿到父容器推荐的宽和高以及计算模式
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
//然后开始测量孩子 必须写
measureChildren(widthMeasureSpec,heightMeasureSpec);
//测量孩子 一定要找到孩子中最高的一个 找到之后 赋值给mChildMaxHeight;
findMaxChild();
//初始化值
int left=0,top=0;
//循环遍历所有的孩子
Log.i("dj", "count is " + getChildCount());
for (int i = 0; i < getChildCount(); i++) {
//再次得到孩子的view
View view = getChildAt(i);
//因为定义left是0 不等于0 就是有东西过来了 就是一个新的一行
if(left!=0){
//好 开始判断我这个view的宽度 是不是比我 父类的宽度还要大 成立了 就是放不下了
if((left+view.getMeasuredWidth())>sizeWidth){
//换行了 然后开始计算下一行的top
//下一行的高 就是最高的子类的高+默认给的上下宽高 也就是20
top += mMaxChildHeight + mVSpace;
//然后让left 清空 继续新的开头
left=0;
}
}
//如果不是新的一行开始
left+=view.getMeasuredWidth()+mHSpace;
}
//赋值
Log.i("dj", "top is " + top);
setMeasuredDimension(sizeWidth,
(top+mMaxChildHeight)>sizeHeight?sizeHeight:top+mMaxChildHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findMaxChild();
//初始化值
int left=0,top=0;
//循环遍历所有的孩子
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
//再次得到孩子的view
View view = getChildAt(i);
//因为定义left是0 不等于0 就是有东西过来了 就是一个新的一行
if(left!=0){
//好 开始判断我这个view的宽度 是不是比我 父类的宽度还要大 成立了 就是放不下了
if((left+view.getMeasuredWidth())>getWidth()){
//换行了 然后开始计算下一行的top
//下一行的高 就是最高的子类的高+默认给的上下宽高 也就是20
top += mMaxChildHeight + mVSpace;
//然后让left 清空 继续新的开头
left=0;
}
}
view.layout(left,top,left+view.getMeasuredWidth(),top+mMaxChildHeight);
left+=view.getMeasuredWidth()+mHSpace;
}
}
private void findMaxChild() {
//规定一个值 用来接收
mMaxChildHeight = 0;
//获得孩子的总个数
int childCount = getChildCount();
//循环取出比较
for (int i = 0; i < childCount; i++) {
//得到一个孩子的view
View view = getChildAt(i);
//判断这个view 的高 比我定义的还大 就把它给我定义的值
if(view.getMeasuredHeight()>mMaxChildHeight){
mMaxChildHeight=view.getMeasuredHeight();
}
}
//循环判断出来 只会得到一个 最大的值 我们的方法也就结束了
}
}
MainActivity
//清空记录的监听
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertView.removeAllViews();
}
});
//搜索的按钮 也就是添加
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView = new TextView(MainActivity.this);
textView.setTextSize(21);
textView.setBackgroundResource(R.drawable.shape);
textView.setText(e_insert.getText());
textView.setTextColor(Color.RED);
insertView.addView(textView);
}
});
圆角 边框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fefefe" />
<stroke android:width="1dip"
android:color="#ccc" />
<corners android:radius="20dp"/>
<padding android:bottom="5dp"
android:left="8dp"
android:right="8dp"
android:top="5dp"/>
</shape>