Skip to content

Instantly share code, notes, and snippets.

@inferjay
Created November 8, 2013 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inferjay/7365905 to your computer and use it in GitHub Desktop.
Save inferjay/7365905 to your computer and use it in GitHub Desktop.
Add the layout of the child view can automatically wrap
public class AutoLineFeedLayout extends RelativeLayout {
// Space between child views.
private final static int PAD_H = 5, PAD_V = 5;
private int mHeight;
/**
*
* <p>Description: 构造方法</p>
*@param context 上下文
*
* @since 2012-8-14 上午11:44:58
* @author inferjay
*/
public AutoLineFeedLayout(Context context) {
super(context);
}
/**
*
* <p>Description: 构造方法</p>
*
*@param context 上下文
*@param attrs 属性集
*
* @since 2012-8-14 上午11:44:58
* @author inferjay
*/
public AutoLineFeedLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
*
* <p>Description: 测量控件大小</p>
* @param widthMeasureSpec 宽
* @param heightMeasureSpec 高
*
* @since 2012-10-25 下午03:17:16
* @author inferjay
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
final int count = getChildCount();//获取子元素个数
int xpos = getPaddingLeft();//获取左内边距
int ypos = getPaddingTop();//获取top内边距
int childHeightMeasureSpec;
if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
else
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
mHeight = 0;
for(int i = 0; i < count; i++) {
final View child = getChildAt(i);
if(child.getVisibility() != GONE) {
child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);
final int childw = child.getMeasuredWidth();
mHeight = Math.max(mHeight, child.getMeasuredHeight() + PAD_V);
if(xpos + childw > width) {
xpos = getPaddingLeft();
ypos += mHeight;
}
xpos += childw + PAD_H;
}
}
if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
height = ypos + mHeight;
} else if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
if(ypos + mHeight < height) {
height = ypos + mHeight;
}
}
// height += 5; // Fudge to avoid clipping bottom of last row.
// Logger.print("recipitents", "height =" + height);
setMeasuredDimension(width, height);
}
/**
*
* <p>Description: 绘制子控件布局</p>
* @param changed 是否改变
* @param l 左
* @param t 上
* @param r 右
* @param b 底
*
* @since 2012-10-25 下午03:18:01
* @author inferjay
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int width = r - l;
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
for(int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
if(child.getVisibility() != GONE) {
final int childw = child.getMeasuredWidth();
final int childh = child.getMeasuredHeight();
if(xpos + childw > width) {
xpos = getPaddingLeft();
ypos += mHeight;
}
child.layout(xpos, ypos, xpos + childw, ypos + childh);
xpos += childw + PAD_H;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment