int itemNum = 25 local eachCol = 5 local rowNum = itemNum / eachCol float parentW = parent:getContentSize():width float parentH = parent:getContentSize():Height for i=1,itemNum do local row = math.ceil( i / eachCol ) local col = n% eachCol if col == 0 then col = eachCol end local item= ccui.ImageView:create('UI/XXXX.png') local itemW= item:getContentSize().width local itemH= item:getContentSize().height local gapW = (parentW - itemW * eachCol) / (eachCol - 1) + itemW -- 间隔总宽度 = 总宽-格子占的宽 , 每个间隔宽度 = 间隔总宽度 / 间隔数 + itenW local gapH = (parentH - itemH * rowNum) / (rowNum - 1) + itemH local x = itemW / 2 local y = itemH / 2 item:setPosition( x + (col-1)*gapW , y + (row-1)*gapH) parent:add(item) end
根据item本身x加上间隔来调整位置。