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 }

 

相关文章:

  • 2021-06-01
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-11-29
  • 2022-12-23
  • 2022-02-19
猜你喜欢
  • 2021-05-18
  • 2022-01-06
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
相关资源
相似解决方案