Js_Ly
//测量 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int highSize = MeasureSpec.getSize(heightMeasureSpec); int highMode = MeasureSpec.getMode(heightMeasureSpec); int width = Math.min(widthSize,highSize); //获取最小值来实现正方形布局 //防止出现尺寸为0不显示 if (widthMode == MeasureSpec.UNSPECIFIED){ width = highSize; }else if (highMode == MeasureSpec.UNSPECIFIED){ width = widthSize; } setMeasuredDimension(width, width); //设置宽高 即正方形布局