du3932066 2015-06-30 08:18
浏览 27
已采纳

如何在GXUI中设置LinearLayout的大小

I'm trying to make a grid with GXUI, I use LinearLayout for rows and columns, but I have a problem with resizing it.

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:

This is the result

Update: GXUI has TableLayout now

  • 写回答

1条回答 默认 最新

  • dpd2349 2015-07-07 20:32
    关注

    You don't need to resize anything. Setting up the LinearLayouts as you are trying to do should have the same effect, but you are making each LinearLayout contain a single label. To make a 4x4 grid, you want one row of 4 columns, with each column having 4 labels. That will make everything grid nicely. Here is code that does that.

    code:

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错