假设我们用CustomView继承ViewGroup,
在xml文件中,假设CustomView里面的23个childView,那么在生成CustomView的对象时自动回调23次父类ViewGroup的generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p);
上面的规律告诉我们,有多少个childView就调用多少次这两个方法:generateLayoutParams(AttributeSet attributeSet)和checkLayoutParams(ViewGroup.LayoutParams p),通过进一步研究我们发现,generateLayoutParams(AttributeSet attributeSet)方法中的attributeSet就是childView的属性集,所以如果我们要对childView加入自定义属性时,在CustomView中重写generateLayoutParams(AttributeSet attributeSet) 方法;
@Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
return new LayoutParams(getContext(), attributeSet);
}
上面的LayoutParams类就是CustomView的内部静态类,示例代码:
public static class LayoutParams extends ViewGroup.LayoutParams {
private static int NO_SPACING = -1;
private int x;
private int y;
private int horizontalSpacing = NO_SPACING;
private int verticalSpacing = NO_SPACING;
private boolean newLine = false;
public LayoutParams(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this.readStyleParameters(context, attributeSet);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams layoutParams) {
super(layoutParams);
}
public boolean horizontalSpacingSpecified() {
return horizontalSpacing != NO_SPACING;
}
public boolean verticalSpacingSpecified() {
return verticalSpacing != NO_SPACING;
}
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
private void readStyleParameters(Context context, AttributeSet attributeSet) {
TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.FlowLayout_LayoutParams);
try {
horizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing, NO_SPACING);
verticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_verticalSpacing, NO_SPACING);
newLine = a.getBoolean(R.styleable.FlowLayout_LayoutParams_layout_newLine, false);
} finally {
a.recycle();
}
}
}
其中用到的声明自定义属性集的文件attrs.xml代码为:
<resources>
<declare-styleable name="FlowLayout">
<attr name="horizontalSpacing" format="dimension"/>
<attr name="verticalSpacing" format="dimension"/>
<attr name="orientation" format="enum">
<enum name="horizontal" value="0"/>
<enum name="vertical" value="1"/>
</attr>
<attr name="debugDraw" format="boolean" />
</declare-styleable>
<declare-styleable name="FlowLayout_LayoutParams">
<attr name="layout_newLine" format="boolean"/>
<attr name="layout_horizontalSpacing" format="dimension"/>
<attr name="layout_verticalSpacing" format="dimension"/>
</declare-styleable>
</resources>
当给childView加入属性后,如果childView调用getLayoutParams(),一定要记得LayoutParams lp = (LayoutParams) child.getLayoutParams();
而CustomView当通过代码去addView时,如果被add的View不调用setLayoutParams,则回调父类ViewGroup的generateLayoutParams()和checkLayoutParams(ViewGroup.LayoutParams p);如果被add的View调用setLayoutParams,则只回调父类ViewGroup的checkLayoutParams(ViewGroup.LayoutParams p);
参考:https://github.com/ApmeM/android-flowlayout