local scrW = winSize.width
local scrH = headImgY - headImgH - 20--scrollView的高度
local scrVsize = cc.size(scrW,scrH)--scrollview的大小
local scrollView = ccui.ScrollView:create()--创建scrollview
scrollView:addTo(mCurLayer)
/**
* ScrollView scroll direction type.
* ScrollView的滚动方向
*/
enum class Direction
{
NONE,
VERTICAL,
HORIZONTAL,
BOTH
};
scrollView:setDirection(1)--设置滚动方向
scrollView:setAnchorPoint(0,0)
scrollView:setPosition(0,0)
scrollView:setContentSize(scrVsize)--设置scrollview的大小,即显示框的大小
local num = 20--button的数量
local dis = 0.05 * winSize.width--间隔
local button = ccui.Button:create(G_GetPlazaRes("Personal/bag_bxk.png"))
local btnW = (winSize.width - 4 * dis) / 3
local btnH = button:getContentSize().height / button:getContentSize().width * btnW
local sSize = cc.size(winSize.width,dis + (btnH+dis)*(math.ceil(num/3)))--根据内容计算内部容器的大小
if sSize.height <= scrVsize.height then
sSize = scrVsize--保证容器的大小要>=scrollView的大小
end
scrollView:setInnerContainerSize(sSize)--scrollview内部容器的大小
local orgY = sSize.height - dis
local y = orgY--button的y值
for i=1,num do
local button = ccui.Button:create(G_GetPlazaRes("Personal/bag_bxk.png"))
scrollView:addChild(button)
button:setScale9Enabled(true)
button:setAnchorPoint(0,1)
local btnW = (winSize.width - 4 * dis) / 3
btnH = button:getContentSize().height / button:getContentSize().width * btnW
local x = dis + (i - 1) % 3 * (btnW + dis)
if (i-1) % 3 == 0 then
y = orgY - ((i-1) / 3) * (dis + btnH)
end
button:setPosition(x,y)
button:setContentSize(btnW,btnH)
end
效果图: