1.MyViewGroup.java
package com.liu.test;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.RequiresApi;
//自定义ViewGroup 类似流式布局
public class MyViewGroup extends ViewGroup {
int allWidth, maxHeight,maxWidth;//ViewGroup宽高
int totalWidth;//统计这一排的宽
int childWidth;//子View的宽
int childHeight;//这一排的子View最高的
public MyViewGroup(Context context) {
super(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//流式布局 一排一排的放下去 如果这一排放不下酒放下一排
maxHeight = 0;
int child = getChildCount();
View childView;
MarginLayoutParams marginLayoutParams;
childHeight = 0;
childWidth = 0;
allWidth = MeasureSpec.getSize(widthMeasureSpec);//可能后期有问题
for (int i = 0; i < child; i++) {
childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
marginLayoutParams = (MarginLayoutParams) childView.getLayoutParams();
childWidth = marginLayoutParams.width + marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;
if (allWidth >= totalWidth + childWidth) {//如果这一行放得下
totalWidth += childWidth;
childHeight = Math.max(childHeight, marginLayoutParams.height +
marginLayoutParams.topMargin + marginLayoutParams.bottomMargin);
//获取最高的数据 比如第一次 为200 第二次为200 第三次放不下了(添加第二次的数据)
//如果结束了 则获取最大
} else {//放不下
maxHeight += childHeight;
childHeight = 0;
childHeight = Math.max(childHeight, marginLayoutParams.height +
marginLayoutParams.topMargin + marginLayoutParams.bottomMargin);
totalWidth = childWidth;
}
maxWidth=Math.max(maxWidth,totalWidth);
end(i, child);
}
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) {
maxWidth = MeasureSpec.getSize(widthMeasureSpec);
} else {
maxWidth = Math.min(MeasureSpec.getSize(widthMeasureSpec), maxWidth);
}
// allWidth = allWidth;
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
maxHeight = MeasureSpec.getSize(heightMeasureSpec);
} else {
maxHeight = Math.min(MeasureSpec.getSize(heightMeasureSpec), maxHeight);
}
setMeasuredDimension(maxWidth, maxHeight);
}
void end(int i, int child) {
if (i == child - 1) {//结束了
maxHeight += childHeight;
childHeight = 0;
totalWidth = 0;
}
}
//流式布局
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int child = getChildCount();
int allTop = 0;
int cHeight = 0;
int allLeft = 0;
int allRight = 0;
for (int i = 0; i < child; i++) {
View childView = getChildAt(i);
final ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) childView.getLayoutParams();
int right = childView.getMeasuredWidth() + marginLayoutParams.leftMargin;
int height = childView.getMeasuredHeight();
int left = marginLayoutParams.leftMargin;
int top = marginLayoutParams.topMargin;
if (allWidth >= allRight + (right + marginLayoutParams.rightMargin)) {//放得下 相等于线性布局的水平方向
allLeft += left;//0~ 330
allRight += right;//0 ~275
allTop += top;//385 ~ 715
childView.layout(allLeft, allTop, allRight, allTop + height);
allLeft += right + marginLayoutParams.rightMargin - left;
allRight += marginLayoutParams.rightMargin;
//取这一行的最大高度
allTop = allTop - top;//减去前面的高度
} else {//放不下 相等于线性布局的垂直方向
allLeft = left;
allRight = right;
allTop += cHeight + marginLayoutParams.topMargin;
childView.layout(allLeft, allTop, allRight, allTop + height);
allTop = allTop - marginLayoutParams.topMargin;//减去topMargin
allLeft += right + marginLayoutParams.rightMargin - left;
allRight = right + marginLayoutParams.rightMargin;
cHeight=0;
}
cHeight = Math.max(cHeight, marginLayoutParams.height
+ marginLayoutParams.topMargin
+ marginLayoutParams.bottomMargin);//获取最高的数据
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
}
2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.liu.test.MyViewGroup
android:background="@color/colorBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"
android:background="@color/colorRed"
android:gravity="center"
android:text="1"
android:textColor="@color/colorPrimary"
android:textSize="16dp" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="-1"
android:textColor="@color/colorRed"
android:textSize="16dp" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"
android:background="@color/colorRed"
android:gravity="center"
android:text="2"
android:textColor="@color/colorPrimary"
android:textSize="16dp" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="-2"
android:textColor="@color/colorRed"
android:textSize="16dp" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="-3"
android:textColor="@color/colorRed"
android:textSize="16dp" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="-5"
android:textColor="@color/colorRed"
android:textSize="16dp" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"
android:background="@color/colorRed"
android:gravity="center"
android:text="3"
android:textColor="@color/colorPrimary"
android:textSize="16dp" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="8dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="-5"
android:textColor="@color/colorRed"
android:textSize="16dp" />
</com.liu.test.MyViewGroup>
</LinearLayout>
3.效果图
![](https://img.haomeiwen.com/i12089562/417b5fde3a1cb659.jpg)
网友评论