1 public class SquareLayout extends RelativeLayout {
2 public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
3 super(context, attrs, defStyle);
4 }
5
6 public SquareLayout(Context context, AttributeSet attrs) {
7 super(context, attrs);
8 }
9
10 public SquareLayout(Context context) {
11 super(context);
12 }
13
14 @Override
15 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
16 setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
17 getDefaultSize(0, heightMeasureSpec));
18 int childWidthSize = getMeasuredWidth();
19 int childHeightSize = getMeasuredHeight();
20 // 高度和宽度一样
21 heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(
22 childWidthSize, MeasureSpec.EXACTLY);
23 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
24 }
25 }