在自定义ViewGroup中,也需要传递一些特殊布局参数,可以通过继承ViewGroup.LayoutParam来实现,不如:
public class MyViewGroup extends ViewGroup {
private int mWidth;
private int mHeight;
public static class LayoutParams extends ViewGroup.LayoutParams {
public int mX;
public int mY;
public LayoutParams(int width, int height, int x, int y) {
super(width, height);
mX = x;
mY = y;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
for ( int i = 0; i < count; ++i ) {
View v = this.getChildAt(i);
LayoutParams lp = (LayoutParams)v.getLayoutParams();
v.layout(lp.mX, lp.mY, lp.mX + lp.width, lp.mY + lp.height);
}
}
在什么地方传递这些参数呢?
MyViewGroup mvg = new MyViewGroup(this)
MyViewGroup.LayoutParams param = new MyViewGroup.LayoutParams(20,34, 12, 11);
mvg.addView(v, param);