根据浮生梦老师前端课程内容,要实现按钮分两排显示,需要调整按钮的布局逻辑。以下是修改后的代码:
```lua
local handle
local top_icon_cfg = {
{name="btn_icon_1",idx=1,icon = "100114", wnd = ssrConstCfg.Box996},--996盒子
{name="btn_icon_2",idx=2, icon = "100113", wnd = ssrConstCfg.TradingBank},--交易行
{name="btn_icon_3",idx=3, icon = "100103", wnd = ssrObjCfg.SevenDaySignin},--福利大厅
{name="btn_icon_4",idx=4, icon = "100104", wnd = ssrObjCfg.QuanFuHD}, --全服活动
{name="btn_icon_5",idx=5, icon = "100107", wnd = ssrObjCfg.HuiYuan}, --会员
{name="btn_icon_11",idx=11, icon = "100111", wnd = ssrObjCfg.WorldBoss}, --世界BOSS
{name="btn_icon_6",idx=6, icon = "100109", wnd = ssrObjCfg.nianKaTeQuan}, --年卡
{name="btn_icon_7",idx=7, icon = "100112", wnd = ssrObjCfg.ShaiWang}, --骰王
{name="btn_icon_8",idx=9, icon = "100110", wnd = ssrObjCfg.ChongZhi}, --充值
{name="btn_icon_9",idx=8, icon = "100106", wnd = ssrObjCfg.ShouChong }, --首充
{name="btn_icon_10",idx=10, icon = "100112", wnd = ssrObjCfg.XinRenTeHui}, --新人特惠
{name="btn_icon_12",idx=12, icon = "100105", wnd = ssrObjCfg.JieRiTeHui}, --节日特惠
}
function ssrUIManager:createTopIcon()
local parent = GUI:Win_FindParent(101)
if parent then
GUI:removeAllChildren(parent)
-- --箭头
ssrUIManager.top_btn = GUI:Button_Create(parent, "btn_switch", 0, -27, "res/official/100079.png")
local count=0
local row=0
local itemsPerRow=6 -- 每行显示6个按钮
for k, val in pairs(top_icon_cfg) do
if val.icon then
local reminValue=-1
local isCreate=true
local key=val.name
-- 原有的条件判断逻辑保持不变
if key == "btn_icon_5" then
isCreate=HuiYuan.hylv < 4
elseif key == "btn_icon_9" then
isCreate=ShouChong.lqsc ~= 1
elseif key == "btn_icon_10" then
local nowtime=SL:GetNowTime()
local createTime=SL:GetMetaValue("SERVER_VALUE", "U0")
createTime=tonumber(createTime) or 0
local endTime=(createTime+1*60*60)+5
local treminValue=endTime-nowtime
local value=SL:GetMetaValue("SERVER_VALUE", "{142}") or ""
value=tonumber(value) or 0
isCreate=value==0 and treminValue>0
reminValue=treminValue
elseif key=="btn_icon_8" then
isCreate=true
local nowtime=SL:GetNowTime()
local createTime=SL:GetMetaValue("SERVER_VALUE", "U0")
createTime=tonumber(createTime) or 0
local endTime=(createTime+6*60*60)+5
local treminValue=endTime-nowtime
local value=SL:GetMetaValue("SERVER_VALUE", "{116}") or ""
value=tonumber(value) or 0
if value==0 and treminValue>0 then
reminValue=treminValue
end
elseif key=="btn_icon_11" then
isCreate=HuiYuan.hylv > 3
elseif key=="btn_icon_12" then
local nowtime=SL:GetNowTime()
local createTime=SL:GetMetaValue("SERVER_VALUE", "U0")
createTime=tonumber(createTime) or 0
local endTime=(createTime+1*60*60)+5
local treminValue=endTime-nowtime
local value=SL:GetMetaValue("SERVER_VALUE", "{142}") or ""
value=tonumber(value) or 0
isCreate=not(value==0 and treminValue>0)
end
if isCreate then
-- 计算行和列
local col = count % itemsPerRow
row = math.floor(count / itemsPerRow)
-- 计算x和y坐标
local x = (col * 87) + 50 -- 水平间距87
local y = -80 - (row * 80) -- 垂直间距80,第一行-80,第二行-160
handle = GUI:Button_Create(parent, key, x, y, "res/official/"..val.icon..".png")
GUI:Button_loadTextures(handle,"res/official/"..val.icon..".png")
GUI:addOnClickEvent(handle, function()
if SL:HasBuff(1100393) then
return SL:ShowSystemTips("您的角色已被封禁!")
end
ssrUIManager:OPEN(val.wnd)
end)
if key=="btn_icon_12" and JieRiTeHui and JieRiTeHui.setRPShow then
JieRiTeHui.setRPShow()
end
if reminValue>0 then
local handle_size=GUI:getContentSize(handle)
local Text_time = GUI:Text_Create(handle, "Text_time", handle_size.width/2, -10, 12, "#00FF00", "")
GUI:setAnchorPoint(Text_time,0.5,0.5)
GUI:Text_COUNTDOWN(Text_time, reminValue, function()
ssrUIManager:createTopIcon()
end)
end
count=count+1
end
end
end
end
end
```
主要修改点:
1. **添加了布局变量**:
- `itemsPerRow = 6`:每行显示6个按钮
- `row`:记录当前行数
2. **计算行列位置**:
- `col = count % itemsPerRow`:计算当前列
- `row = math.floor(count / itemsPerRow)`:计算当前行
3. **调整坐标计算**:
- `x = (col * 87) + 50`:水平位置根据列数计算
- `y = -80 - (row * 80)`:垂直位置根据行数计算,每行间距80像素
这样修改后,按钮会按照6个一行的方式排列,超过6个会自动换到第二行。你可以根据需要调整`itemsPerRow`的值来控制每行显示的按钮数量。