fyne 自定义宽度和高度

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))
			}
		}
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值