我正在尝试使用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)
}
当我运行它时,我会看到这个窗口:
如何让窗口有一个滚动条,以便我可以查看所有行?
答案 0 :(得分:2)
我无法使用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)
}
每行都放在列表中,它们的数量和大小在重写的方法中指定。优点是列表中已经有滚动条。
答案 1 :(得分:0)
以下代码使用ScrollLayout向窗口添加滚动条。诀窍是让ScrollLayout成为窗口的子节点,并使下一个小部件(在本例中为LinearLayout)成为ScrollLayout的子节点。
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)
sl := theme.CreateScrollLayout()
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)
}
sl.SetChild(row)
window.AddChild(sl)
}
func main() {
gl.StartDriver(appMain)
}
请注意,当我增加行数(最右边的列开始被切断)时,我的计算机给了我显示问题,但是其他人没有遇到这个问题,所以很可能是因为我的安装不好