Fyne 实现对widget自定义宽度和高度
1、 由于fyne默认实现layout中,widget的宽度和高度为最小化大小,所以当即使Resize() widget 更然不能在container中实现自定义大小;
2、通过修改layout中实现方法,去除最小化widget的行为,可以实现widget的自定义大小
3、仅仅对MultiEntry进行了自定义大小测试
func (g *boxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := make([]fyne.CanvasObject, 0)
total := float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if g.isSpacer(child) {
spacers = append(spacers, child)
continue
}
if g.horizontal {
total += child.Size().Width
} else {
total += child.Size().Height
}
}
x, y := float32(0), float32(0)
var extra float32
if g.horizontal {
extra = size.Width - total - (theme.Padding() * float32(len(objects)-len(spacers)-1))
} else {
extra = size.Height - total - (theme.Padding() * float32(len(objects)-len(spacers)-1))
}
extraCell := float32(0)
if len(spacers) > 0 {
extraCell = extra / float32(len(spacers))
}
for index, child := range objects {
if !child.Visible() {
continue
}
width := child.Size().Width
height := child.Size().Height
if g.horizontal {
if width < child.MinSize().Width {
width = child.MinSize().Width
}
} else {
if height < child.MinSize().Height {
height = child.MinSize().Height
}
}
if g.isSpacer(child) {
if g.horizontal {
x += extraCell
} else {
y += extraCell
}
continue
}
child.Move(fyne.NewPos(x, y))
if g.horizontal {
x += theme.Padding() + width
// 如果是最后一个控件,则占满剩余控件
if index == len(objects)-1 {
child.Resize(fyne.NewSize(size.Width-x+width, size.Height))
} else {
child.Resize(fyne.NewSize(width, size.Height))
}
} else {
y += theme.Padding() + height
if index == len(objects)-1 {
child.Resize(fyne.NewSize(size.Width,size.Height - y + height))
}else {
child.Resize(fyne.NewSize(size.Width, height))
}
}
}
}