Created
April 29, 2016 08:29
-
-
Save ownwell/b842563c7c1a3e57671898d51bf92020 to your computer and use it in GitHub Desktop.
可动态换行的布局文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* @author Cyning | |
* @since 2016.04.29 | |
* Time 15:56 | |
* Desc <p>类/接口描述</p> | |
*/ | |
public class FlowLayout extends ViewGroup { | |
private float mVerticalSpacing; //每个item纵向间距 | |
private float mHorizontalSpacing; //每个item横向间距 | |
public FlowLayout(Context context) { | |
super(context); | |
} | |
public FlowLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public void setHorizontalSpacing(float pixelSize) { | |
mHorizontalSpacing = pixelSize; | |
} | |
public void setVerticalSpacing(float pixelSize) { | |
mVerticalSpacing = pixelSize; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int selfWidth = resolveSize(0, widthMeasureSpec); | |
int paddingLeft = getPaddingLeft(); | |
int paddingTop = getPaddingTop(); | |
int paddingRight = getPaddingRight(); | |
int paddingBottom = getPaddingBottom(); | |
int childLeft = paddingLeft; | |
int childTop = paddingTop; | |
int lineHeight = 0; | |
//通过计算每一个子控件的高度,得到自己的高度 | |
for (int i = 0, childCount = getChildCount(); i < childCount; ++i) { | |
View childView = getChildAt(i); | |
LayoutParams childLayoutParams = childView.getLayoutParams(); | |
childView.measure( | |
getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, | |
childLayoutParams.width), | |
getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, | |
childLayoutParams.height)); | |
int childWidth = childView.getMeasuredWidth(); | |
int childHeight = childView.getMeasuredHeight(); | |
lineHeight = Math.max(childHeight, lineHeight); | |
if (childLeft + childWidth + paddingRight > selfWidth) { | |
childLeft = paddingLeft; | |
childTop += mVerticalSpacing + lineHeight; | |
lineHeight = childHeight; | |
} else { | |
childLeft += childWidth + mHorizontalSpacing; | |
} | |
} | |
int wantedHeight = childTop + lineHeight + paddingBottom; | |
setMeasuredDimension(selfWidth, resolveSize(wantedHeight, heightMeasureSpec)); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
int myWidth = r - l; | |
int paddingLeft = getPaddingLeft(); | |
int paddingTop = getPaddingTop(); | |
int paddingRight = getPaddingRight(); | |
int childLeft = paddingLeft; | |
int childTop = paddingTop; | |
int lineHeight = 0; | |
//根据子控件的宽高,计算子控件应该出现的位置。 | |
for (int i = 0, childCount = getChildCount(); i < childCount; ++i) { | |
View childView = getChildAt(i); | |
if (childView.getVisibility() == View.GONE) { | |
continue; | |
} | |
int childWidth = childView.getMeasuredWidth(); | |
int childHeight = childView.getMeasuredHeight(); | |
lineHeight = Math.max(childHeight, lineHeight); | |
if (childLeft + childWidth + paddingRight > myWidth) { | |
childLeft = paddingLeft; | |
childTop += mVerticalSpacing + lineHeight; | |
lineHeight = childHeight; | |
} | |
childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); | |
childLeft += childWidth + mHorizontalSpacing; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment