特别好用的util,啥都不说了直接上代码,下面有现在地址哦
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class AutoUtil extends ViewGroup{
public AutoUtil(Context context) {
super(context);
}
public AutoUtil(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoUtil(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean br, int l, int t, int r, int b) {
int topPadding=getPaddingTop();
int rawHeight=0;
int viewWidth=r-l;
int leftOffer=getPaddingLeft();//横向的偏移量
View child;
for (int i=0,count=getChildCount();i<count;i++){
child=getChildAt(i);
if (child.getVisibility()==GONE) continue;
LayoutParams params=child.getLayoutParams();
MarginLayoutParams lp=null;
if (params instanceof MarginLayoutParams){
lp= (MarginLayoutParams) params;
}else {
lp=new MarginLayoutParams(params);
}
int childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
if (childWidth+leftOffer+getPaddingRight()>viewWidth){
leftOffer=getPaddingLeft();
topPadding+=rawHeight;
rawHeight=0;
}
int ll=lp.leftMargin+leftOffer;
int tl=topPadding+lp.topMargin;
int rl=leftOffer+lp.rightMargin+child.getMeasuredWidth();
int bl=topPadding+lp.topMargin+child.getMeasuredHeight();
child.layout(ll,tl,rl,bl);
leftOffer+=childWidth;
int childHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;
rawHeight=Math.max(rawHeight,childHeight);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecSize=MeasureSpec.getSize(widthMeasureSpec);
int widthSpecMode=MeasureSpec.getMode(widthMeasureSpec);
int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);
int heightSpecMode=MeasureSpec.getMode(heightMeasureSpec);
int wedth=0;
int height=0;
int rawWidth=0;
int rawHeight=0;
int count=getChildCount();
for (int i=0;i<count;i++){
View view=getChildAt(i);
if (view.getVisibility()==GONE){
if (i==count-1){
wedth=Math.max(wedth,rawWidth) ;
height+=rawHeight;
}
continue;
}
measureChildWithMargins(view,widthMeasureSpec,0,heightMeasureSpec,0);
LayoutParams params=view.getLayoutParams();
MarginLayoutParams lp=null;
if (params instanceof MarginLayoutParams){
lp= (MarginLayoutParams) params;
}else {
lp=new MarginLayoutParams(params);
}
int viewWight=view.getWidth()+lp.leftMargin+lp.rightMargin;
int viewHeight =view.getHeight()+lp.topMargin+lp.bottomMargin;
if (viewWight+rawWidth>widthMeasureSpec-getPaddingLeft()-getPaddingRight()){
wedth=Math.max(wedth,viewWight);
rawWidth=viewWight;
height=rawHeight;
rawHeight=viewHeight;
}else {
wedth+=viewWight;
height=Math.max(rawHeight,viewHeight);
}
if (i==count-1){
wedth=Math.max(rawWidth,wedth);
height+=rawHeight;
}
}
setMeasuredDimension(widthSpecMode==MeasureSpec.EXACTLY?wedth:widthSpecSize,heightSpecMode==MeasureSpec.EXACTLY?height:heightSpecSize);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(),attrs);
}
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
return new MarginLayoutParams(p);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
}
}
大家赋值黏贴就可以使用喽 下载链接