07-border布局的另一个用处
实现如下的布局:
分析:
1.USERNAME和PASSWORD使用form
2.PASSWORD的文本框使用NewMultiLineEntry
布局1
USERNAME和PASSWORD作为一个form整体,使用border布局,form设置为top,文本框设置为center参数。
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("VPN")
userEntry := widget.NewEntry()
userL := widget.NewFormItem("USERNAME", userEntry)
passEntry := widget.NewPasswordEntry()
passL := widget.NewFormItem("PASSWORD", passEntry)
form := widget.NewForm(userL, passL)
txtArea := widget.NewMultiLineEntry()
c := container.NewBorder(form, nil, nil, nil, txtArea)
myWindow.SetContent(c)
myWindow.Resize(fyne.NewSize(300, 300))
myWindow.Show()
myApp.Run()
}
布局2
使用vbox布局的效果:
代码如下:
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("VPN")
userEntry := widget.NewEntry()
userL := widget.NewFormItem("USERNAME", userEntry)
passEntry := widget.NewPasswordEntry()
passL := widget.NewFormItem("PASSWORD", passEntry)
form := widget.NewForm(userL, passL)
txtArea := widget.NewMultiLineEntry()
//设置大小无效
txtArea.Resize(fyne.NewSize(100, 100))
c := container.NewVBox(form, txtArea)
myWindow.SetContent(c)
myWindow.Resize(fyne.NewSize(300, 300))
myWindow.Show()
myApp.Run()
}
当把txtArea放到容器里面,设置大小是不生效的。要想设置的大小生效,可以设置无布局。
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("VPN")
userEntry := widget.NewEntry()
userL := widget.NewFormItem("USERNAME", userEntry)
passEntry := widget.NewPasswordEntry()
passL := widget.NewFormItem("PASSWORD", passEntry)
form := widget.NewForm(userL, passL)
txtArea := widget.NewMultiLineEntry()
txtArea.Resize(fyne.NewSize(290, 200))
c1 := container.NewWithoutLayout(txtArea)
c := container.NewVBox(form, c1)
myWindow.SetContent(c)
myWindow.Resize(fyne.NewSize(300, 300))
myWindow.Show()
myApp.Run()
}
效果如下: