user-interface - GXUI中如何设置LinearLayout的大小

标签 user-interface go gxui

我正在尝试用 GXUI 制作网格, 我用 LinearLayout对于行和列,但我在调整它的大小时遇到​​了问题。

package main

import (
  "fmt"

  "github.com/google/gxui"
  "github.com/google/gxui/math"
  "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)
  size := window.Viewport().SizeDips()

  grid := theme.CreateLinearLayout()
  grid.SetDirection(gxui.TopToBottom)
  grid.SetSizeMode(gxui.Fill)

  for y := 0; y < 4; y++ {
    row := theme.CreateLinearLayout()
    row.SetDirection(gxui.LeftToRight)

    for x := 0; x < 4; x++ {
      label := theme.CreateLabel()
      label.SetText(fmt.Sprintf("%d", y*4+x))

      cell := theme.CreateLinearLayout()
      cell.SetDirection(gxui.TopToBottom)
      cell.SetSize(math.Size{W: size.W/4, H: size.H/4}) // not actually resizes
      cell.AddChild(label)

      row.AddChild(cell)
    }

    grid.AddChild(row)
  }

  window.AddChild(grid)
}

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

这是结果:

This is the result

更新:GXUI 有TableLayout现在

最佳答案

您不需要调整任何东西的大小。像您尝试的那样设置 LinearLayouts 应该具有相同的效果,但是您正在使每个 LinearLayout 包含一个标签。要制作 4x4 网格,您需要一行 4 列,每列有 4 个标签。这将使一切都很好地网格化。这是执行此操作的代码。

代码:

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 < 4; 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)
}

关于user-interface - GXUI中如何设置LinearLayout的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31133071/

相关文章:

http - 无法使用 http.Get in GO 从 nginx 获取完整文件

go - 守护进程只执行一次 goroutine

go - 为什么不更改 gxui 中进度条的映射?

android - 启动 Activity 时的并发/UI 更新问题

python - QT设计器没有安装uic程序来查看应用程序中的代码

user-interface - Modelica:检查可替换包或模型的相等性

java - GUI 中的窗口引用(?)问题

docker - 去构建 docker : unexpected binary in/go/bin