local n1_chuansong = {}
local list = {
{ name = "比奇城", mapId = "0" },
{ name = "盟重省", mapId = "3" },
{ name = "沙巴克城", mapId = "0150" },
{ name = "白日门", mapId = "11" },
{ name = "苍月岛", mapId = "5" }
}
function n1_chuansong:main()
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-06 18:14:53 |
👁️ 阅读: 1
左侧容器按钮更换选中图片,可以通过 `GUI:Button_loadTextureNormal()` 函数来设置不同状态下的图片。
在你的 `init_main` 函数中,当创建或更新按钮时,根据当前选中的索引 `n1_chuansong.idx` 来决定加载哪张图片。
修改后的 `init_main` 函数如下:
```lua
function n1_chuansong:init_main()
-- 按钮容器初始化
local list_v = self.ui.ListView_1
if list_v then
GUI:ListView_removeAllItems(list_v) -- 使用正确的清空列表函数
end
-- 创建容器内按钮
for k, v in ipairs(list) do
local bt = GUI:Button_Create(self.ui.ListView_1, "bt" .. k, 0, 0, "res/custom/chuansong/an2.png")
GUI:Button_setTitleText(bt, v.name)
GUI:Button_setTitleFontSize(bt, 18)
-- 根据是否选中来设置按钮正常状态的图片
if n1_chuansong.idx == k then
-- 选中状态:加载选中图片
GUI:Button_loadTextureNormal(bt, "res/custom/chuansong/an1.png") -- 假设an1.png是选中图片
GUI:Button_setTitleColor(bt, "#FFFF00")
GUI:Button_setBrightStyle(bt, 1)
else
-- 未选中状态:加载默认图片
GUI:Button_loadTextureNormal(bt, "res/custom/chuansong/an2.png") -- 假设an2.png是默认图片
GUI:Button_setTitleColor(bt, "#A0A0A4")
GUI:Button_setBrightStyle(bt, 0)
end
-- 添加按钮点击事件
GUI:addOnClickEvent(bt, function()
n1_chuansong.idx = k -- 更新选中索引
n1_chuansong.p1 = k -- 更新传送参数
self:init_main() -- 刷新所有按钮状态
end)
-- 将按钮添加到列表容器中
GUI:ListView_pushBackCustomItem(self.ui.ListView_1, bt)
end
end
```
**关键修改点:**
1. **清空列表**:使用 `GUI:ListView_removeAllItems()` 而不是 `GUI:removeAllChildren()`
2. **图片切换**:使用 `GUI:Button_loadTextureNormal(bt, "图片路径")` 来设置按钮的正常状态图片
3. **添加按钮到容器**:使用 `GUI:ListView_pushBackCustomItem()` 将按钮添加到列表容器中
**图片路径说明:**
- `an1.png`:选中状态的按钮图片
- `an2.png`:未选中状态的按钮图片
这样当点击不同按钮时,选中的按钮会显示 `an1.png`,其他按钮显示 `an2.png`,实现了图片切换效果。
记得在 `init_main` 函数最后调用一下刷新布局的函数:
```lua
GUI:ListView_doLayout(self.ui.ListView_1)
```
← 返回问答库