方形布局SquareLayout

 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 }

 

posted @ 2016-01-08 11:29  望臻风格  阅读(862)  评论(0编辑  收藏  举报