Grid Layout

The grid layout lays out the elements of a container in a grid pattern with a fixed number of columns. Items will fill a single row until the number of columns is met, after this a new row will be created. Vertical space will be split equally between each of the rows of objects.

You create a grid layout using layout.NewGridLayout(cols) where cols is the number of items (columns) you wish to have in each row. This layout is then passed as the first parameter to fyne.NewContainerWithLayout(...).

If you resize the container then each of the cells will resize equally to share the available space.

Example Code

            
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
	"image/color"

	"fyne.io/fyne"
	"fyne.io/fyne/app"
	"fyne.io/fyne/canvas"
	"fyne.io/fyne/layout"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("Grid Layout")

	text1 := canvas.NewText("1", color.White)
	text2 := canvas.NewText("2", color.White)
	text3 := canvas.NewText("3", color.White)
	grid := fyne.NewContainerWithLayout(layout.NewGridLayout(2),
		text1, text2, text3)
	myWindow.SetContent(grid)
	myWindow.ShowAndRun()
}