如何在GXUI中设置LinearLayout的大小

时间:2015-06-30 08:18:49

标签: 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

1 个答案:

答案 0 :(得分:0)

您不需要调整任何大小。正如您尝试的那样设置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)
}