1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| public class CustomViewGroup extends ViewGroup {
public CustomViewGroup(Context context) { super(context); }
public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); }
public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec);
for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); int mode = MeasureSpec.EXACTLY; int childWidth = widthSize / 4; int childHeight = heightSize / 4; child.measure(MeasureSpec.makeMeasureSpec(childWidth, mode), MeasureSpec.makeMeasureSpec(childHeight, mode)); } }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); int gravity = lp.gravity; switch (gravity) { case LayoutParams.TOPLEFT_GRAVITY: child.layout(l, t, width, height); break; case LayoutParams.TOPRIGHT_GRAVITY: child.layout(r - width, t, r, height); break; case LayoutParams.BOTTOMLEFT_GRAVITY: child.layout(l, b - height, width, b); break; case LayoutParams.BOTTOMRIGHT_GRAVITY: child.layout(r - width, b - height, r, b); break; case LayoutParams.CENTER_GRAVITY: child.layout((r - width) / 2, (b - height) / 2, (r + width) / 2, (b + height) / 2); break; default: break; } } }
@Override public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { return new LayoutParams(getContext(), attrs); }
@Override protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { return new LayoutParams(p); }
@Override protected ViewGroup.LayoutParams generateDefaultLayoutParams() { return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1); }
@Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { return p instanceof LayoutParams; }
public static class LayoutParams extends ViewGroup.LayoutParams { public static final int UNSPECIFIED_GRAVITY = -1; public static final int TOPLEFT_GRAVITY = 1; public static final int TOPRIGHT_GRAVITY = 2; public static final int BOTTOMLEFT_GRAVITY = 3; public static final int BOTTOMRIGHT_GRAVITY = 4; public static final int CENTER_GRAVITY = 5;
public int gravity = UNSPECIFIED_GRAVITY;
public LayoutParams(@NonNull Context c, @Nullable AttributeSet attrs) { super(c, attrs);
final TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.CustomViewGroup); gravity = a.getInt(R.styleable.CustomViewGroup_custom_gravity, UNSPECIFIED_GRAVITY); a.recycle(); }
public LayoutParams(int width, int height, int gravity) { super(width, height); this.gravity = gravity; }
public LayoutParams(@NonNull ViewGroup.LayoutParams source) { super(source); this.gravity = TOPLEFT_GRAVITY; }
public LayoutParams(@NonNull LayoutParams source) { super(source);
this.gravity = source.gravity; } }
}
|