gpt4 book ai didi

scroll - 使用 GXUI 在 Go 中使窗口滚动

转载 作者:IT王子 更新时间:2023-10-29 02:05:12 26 4
gpt4 key购买 nike

我正在尝试使用 GXUI 在我的 Go 应用程序窗口中添加滚动条。

假设我有这段代码:

package main

import (
"fmt"

"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)

func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)

window := theme.CreateWindow(800, 600, "Grid")
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)

row := theme.CreateLinearLayout()
row.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
for r := 0; r < 100; r++ {
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", r*4+c))
col.AddChild(cell)
}
row.AddChild(col)
}

window.AddChild(row)
}

func main() {
gl.StartDriver(appMain)
}

当我运行它时,我得到这个窗口:

enter image description here

如何让窗口有滚动条以便我可以查看所有行?

最佳答案

我无法使用 help ScrollLayout 来完成,但我可以根据 github 中的示例提出此变体.

package main

import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/math"
"github.com/google/gxui/samples/flags"
"github.com/google/gxui/themes/dark"
)

type customAdapter struct {
gxui.AdapterBase
}

func (a *customAdapter) Count() int {
return 1000
}

func (a *customAdapter) ItemAt(index int) gxui.AdapterItem {
return index
}

func (a *customAdapter) ItemIndex(item gxui.AdapterItem) int {
return item.(int)
}

func (a *customAdapter) Size(theme gxui.Theme) math.Size {
return math.Size{W: 200, H: 25}
}

func (a *customAdapter) Create(theme gxui.Theme, index int) gxui.Control {

layout1 := theme.CreateLinearLayout()
layout1.SetDirection(gxui.LeftToRight)
for c := 0; c < 4; c++ {
col := theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
cell := theme.CreateLabel()
cell.SetText(fmt.Sprintf("%d", index*4+c))
col.AddChild(cell)
layout1.AddChild(col)
}
return layout1
}

func appMain(driver gxui.Driver) {
theme := dark.CreateTheme(driver)
window := theme.CreateWindow(600, 400, "Grid")
window.BorderPen()
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(driver.Terminate)
adapter := &customAdapter{}
list := theme.CreateList()
list.SetAdapter(adapter)
list.SetOrientation(gxui.Vertical)
window.AddChild(list)
}

func main() {
gl.StartDriver(appMain)
}

每一行都放在列表中,它们的数量和大小在覆盖的方法中指定。优点是列表中已经有了滚动条。

关于scroll - 使用 GXUI 在 Go 中使窗口滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com