package com.hfengxiang.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class OverlappingLinearLayout extends LinearLayout {
public OverlappingLinearLayout(Context context) {
super(context);
}
public OverlappingLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public OverlappingLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 测量子视图
measureChildren(widthMeasureSpec, heightMeasureSpec);
int totalWidth = 0;
int totalHeight = 0;
int maxWidth = 0;
int maxHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
totalWidth += child.getMeasuredWidth();
totalHeight += child.getMeasuredHeight();
maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
}
if (getOrientation() == HORIZONTAL) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(Math.min(totalWidth, parentWidth), maxHeight);
} else { // VERTICAL
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(maxWidth, Math.min(totalHeight, parentHeight));
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left = 0;
int top = 0;
int totalWidth = 0;
int totalHeight = 0;
int measuredWidth = getMeasuredWidth();
int measuredHeight = getMeasuredHeight();
if (getOrientation() == HORIZONTAL) {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
totalWidth += child.getMeasuredWidth();
}
int overlaps = totalWidth - measuredWidth;
// 布局子View(横向)
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
int subOverlaps = Math.max(0, overlaps / (getChildCount() - 1));
child.layout(left, top, left + childWidth, top + childHeight);
left += (childWidth - subOverlaps); // 下一个子View的左边位置
}
} else { // VERTICAL
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
totalHeight += child.getMeasuredHeight();
}
int overlaps = totalHeight - measuredHeight;
// 布局子View(纵向)
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
int subOverlaps = Math.max(0, overlaps / (getChildCount() - 1));
child.layout(left, top, left + childWidth, top + childHeight);
top += (childHeight - subOverlaps); // 下一个子View的顶部位置
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://sch