iis服务器助手广告
返回顶部
首页 > 资讯 > 精选 >自定义 Fyne 自适应网格布局
  • 920
分享到

自定义 Fyne 自适应网格布局

2024-02-12 16:02:46 920人浏览 安东尼
摘要

问题内容 我正在修改fyne库的container.newadaptivegrid(),以便根据我们传递的比例切片渲染小部件的宽度。截至目前,container.newadaptive

问题内容

我正在修改fyne库的container.newadaptivegrid(),以便根据我们传递的比例切片渲染小部件的宽度。截至目前,container.newadaptivegrid() 在一行中呈现等宽的小部件。基本上(总行大小/现在的小部件)。

我的代码:

package main

import (
    "fmt"
    "math"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/theme"
    "fyne.io/fyne/v2/widget"
)

func New(layout fyne.Layout, objects ...fyne.canvasObject) *fyne.Container {
    return fyne.NewContainerWithLayout(layout, objects...)
}

func NewAdaptiveGridWithRatiOS(ratios []float32, objects ...fyne.CanvasObject) *fyne.Container {
    return New(NewAdaptiveGridLayoutWithRatios(ratios), objects...)
}

// Declare confORMity with Layout interface
var _ fyne.Layout = (*adaptiveGridLayoutWithRatios)(nil)

type adaptiveGridLayoutWithRatios struct {
    ratios          []float32
    adapt, vertical bool
}

func NewAdaptiveGridLayoutWithRatios(ratios []float32) fyne.Layout {
    return &adaptiveGridLayoutWithRatios{ratios: ratios, adapt: true}
}

func (g *adaptiveGridLayoutWithRatios) horizontal() bool {
    if g.adapt {
        return fyne.IsHorizontal(fyne.CurrentDevice().Orientation())
    }

    return !g.vertical
}

func (g *adaptiveGridLayoutWithRatios) countRows(objects []fyne.CanvasObject) int {
    count := 0
    for _, child := range objects {
        if child.Visible() {
            count++
        }
    }

    return int(math.Ceil(float64(count) / float64(len(g.ratios))))
}

// Get the leading (top or left) edge of a grid cell.
// size is the ideal cell size and the offset is which col or row its on.
func getLeading(size float64, offset int) float32 {
    ret := (size + float64(theme.Padding())) * float64(offset)

    return float32(ret)
}

// Get the trailing (bottom or right) edge of a grid cell.
// size is the ideal cell size and the offset is which col or row its on.
func getTrailing(size float64, offset int) float32 {
    return getLeading(size, offset+1) - theme.Padding()
}

// Layout is called to pack all child objects into a specified size.
// For a GridLayout this will pack objects into a table format with the number
// of columns specified in our constructor.
func (g *adaptiveGridLayoutWithRatios) Layout(objects []fyne.CanvasObject, size fyne.Size) {
    rows := g.countRows(objects)
    cols := len(g.ratios)
    if g.horizontal() {
        cols = rows
        rows = len(g.ratios)
    }

    padWidth := float32(cols-1) * theme.Padding()
    padHeight := float32(rows-1) * theme.Padding()
    var totalRatio float32
    for _, r := range g.ratios {
        totalRatio += r
    }

    cellWidth := (float64(size.Width) - float64(padWidth)) / float64(len(g.ratios))
    cellHeight := float64(size.Height-padHeight) / float64(rows)

    if !g.horizontal() {
        cellWidth, cellHeight = cellHeight, cellWidth
        cellWidth = float64(size.Width-padWidth) / float64(rows)
        cellHeight = float64(size.Height-padHeight) / float64(len(g.ratios))
    }

    row, col := 0, 0
    i := 0
    for _, child := range objects {
        if !child.Visible() {
            continue
        }

        //ratio := g.ratios[j%len(g.ratios)]
        cellSize := fyne.NewSize(float32(cellWidth)*g.ratios[i], float32(cellHeight))

        x1 := getLeading(float64(cellSize.Width), col)
        y1 := getLeading(float64(cellSize.Height), row)
        x2 := getTrailing(float64(cellSize.Width), col)
        y2 := getTrailing(float64(cellSize.Height), row)
        fmt.Println("1s :", x1, y1)
        fmt.Println("2s :", x2, y2)
        child.Move(fyne.NewPos(x1, y1))
        child.Resize(cellSize)

        if g.horizontal() {
            if (i+1)%cols == 0 {
                row++
                col = 0
            } else {
                col++
            }
        } else {
            if (i+1)%cols == 0 {
                col++
                row = 0
            } else {
                row++
            }
        }
        i++
    }
    fmt.Println("i :", i)
}

func (g *adaptiveGridLayoutWithRatios) MinSize(objects []fyne.CanvasObject) fyne.Size {
    minSize := fyne.NewSize(0, 0)
    return minSize
}

func main() {
    myApp := app.New()
    myWindow := myApp.NewWindow("My windows")
    myWindow.Resize(fyne.NewSize(600, 200))

    button1 := widget.NewButton("Button 1", func() {
        // Handle button click for button 1
    })

    button2 := widget.NewButton("Button 2", func() {
        // Handle button click for button 2
    })
    button1.Importance = widget.WarningImportance
    button2.Importance = widget.DangerImportance
    title := widget.NewLabelWithStyle("Custom", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})

    myWindow.SetContent(container.NewVBox(title,
        NewAdaptiveGridWithRatios([]float32{0.3, 0.7}, button1, button2)))

    myWindow.ShowAndRun()
}

我希望按钮并排放置,按钮的相对宽度比例为 3:7。但我得到了两条水平线,一条在另一条之下。 我正在修改:https://GitHub.com/fyne-io/fyne/blob/8c2509518b2df442a6b748d9b07754739592e6d7/layout/gridlayout.Go 制作我的定制产品。

解决方法

这有效:

package main

import (
    "fmt"
    "math"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/theme"
    "fyne.io/fyne/v2/widget"
)

func New(layout fyne.Layout, objects ...fyne.CanvasObject) *fyne.Container {
    return fyne.NewContainerWithLayout(layout, objects...)
}

func NewAdaptiveGridWithRatios(ratios []float32, objects ...fyne.CanvasObject) *fyne.Container {
    return New(NewAdaptiveGridLayoutWithRatios(ratios), objects...)
}

// Declare conformity with Layout interface
var _ fyne.Layout = (*adaptiveGridLayoutWithRatios)(nil)

type adaptiveGridLayoutWithRatios struct {
    ratios          []float32
    adapt, vertical bool
}

func NewAdaptiveGridLayoutWithRatios(ratios []float32) fyne.Layout {
    return &adaptiveGridLayoutWithRatios{ratios: ratios, adapt: true}
}

func (g *adaptiveGridLayoutWithRatios) horizontal() bool {
    if g.adapt {
        return fyne.IsHorizontal(fyne.CurrentDevice().Orientation())
    }

    return !g.vertical
}

func (g *adaptiveGridLayoutWithRatios) countRows(objects []fyne.CanvasObject) int {
    count := 0
    for _, child := range objects {
        if child.Visible() {
            count++
        }
    }

    return int(math.Ceil(float64(count) / float64(len(g.ratios))))
}

// Layout is called to pack all child objects into a specified size.
// For a GridLayout this will pack objects into a table format with the number
// of columns specified in our constructor.
func (g *adaptiveGridLayoutWithRatios) Layout(objects []fyne.CanvasObject, size fyne.Size) {

    rows := g.countRows(objects)
    cols := len(g.ratios)

    padWidth := float32(cols-1) * theme.Padding()
    padHeight := float32(rows-1) * theme.Padding()
    tGap := float64(padWidth)
    tcellWidth := float64(size.Width) - tGap
    cellHeight := float64(size.Height-padHeight) / float64(rows)

    fmt.Println(cols, rows)
    fmt.Println(cellHeight, tcellWidth+tGap, tGap)
    fmt.Println("tcellWidth, cellHeight", tcellWidth, cellHeight)
    if !g.horizontal() {
        padWidth, padHeight = padHeight, padWidth
        tcellWidth = float64(size.Width-padWidth) - tGap
        cellHeight = float64(size.Height-padHeight) / float64(cols)
    }

    row, col := 0, 0
    i := 0
    var x1, x2, y1, y2 float32 = 0.0, 0.0, 0.0, 0.0
    fmt.Println("padWidth, padHeight, tcellWidth, cellHeight, float32(theme.Padding()):", padWidth, padHeight, tcellWidth, cellHeight, float32(theme.Padding()))
    for _, child := range objects {
        if !child.Visible() {
            continue
        }

        if i == 0 {
            x1 = 0
            y1 = 0
        } else {
            x1 = x2 + float32(theme.Padding())*float32(1)
            y1 = y2 - float32(cellHeight)
        } // float32(tGap/float64(col))
        //  (size + float64(theme.Padding())) * float64(offset)  float32(theme.Padding())*float32(1)
        x2 = x1 + float32(tcellWidth*float64(g.ratios[i]))
        y2 = float32(cellHeight)

        fmt.Println("x1,y1 :", x1, y1)
        fmt.Println("x2, y2 :", x2, y2)
        fmt.Println("eff width", tcellWidth*float64(g.ratios[i]))

        fmt.Println("------")
        child.Move(fyne.NewPos(x1, y1))
        child.Resize(fyne.NewSize((x2 - x1), y2-y1))

        if g.horizontal() {
            if (i+1)%cols == 0 {
                row++
                col = 0
            } else {
                col++
            }
        } else {
            if (i+1)%cols == 0 {
                col++
                row = 0
            } else {
                row++
            }
        }
        i++
    }
    fmt.Println("i :", i)
}

func (g *adaptiveGridLayoutWithRatios) MinSize(objects []fyne.CanvasObject) fyne.Size {
    rows := g.countRows(objects)
    minSize := fyne.NewSize(0, 0)
    for _, child := range objects {
        if !child.Visible() {
            continue
        }

        minSize = minSize.Max(child.MinSize())
    }

    if g.horizontal() {
        minContentSize := fyne.NewSize(minSize.Width*float32(len(g.ratios)), minSize.Height*float32(rows))
        return minContentSize.Add(fyne.NewSize(theme.Padding()*fyne.Max(float32(len(g.ratios)-1), 0), theme.Padding()*fyne.Max(float32(rows-1), 0)))
    }

    minContentSize := fyne.NewSize(minSize.Width*float32(rows), minSize.Height*float32(len(g.ratios)))
    return minContentSize.Add(fyne.NewSize(theme.Padding()*fyne.Max(float32(rows-1), 0), theme.Padding()*fyne.Max(float32(len(g.ratios)-1), 0)))
}

func main() {
    myApp := app.New()
    myWindow := myApp.NewWindow("My Windows Custom UI")
    myWindow.Resize(fyne.NewSize(600, 200))

    var buttons [16]*widget.Button

    for i := 0; i < 16; i++ {
        button := widget.NewButton(fmt.Sprintf("Btn %d", i+1), func() {
            // Handle button click for this button
        })

        // Set the button importance based on the button index
        if i%2 == 0 {
            button.Importance = widget.WarningImportance
        } else {
            button.Importance = widget.DangerImportance
        }

        buttons[i] = button
    }

    pgBar := widget.NewLabelWithStyle("Progress :", fyne.TextAlignCenter, fyne.TextStyle{Italic: true})
    progressBar := widget.NewProgressBar()
    progressBar.SetValue(0.95)

    myWindow.SetContent(container.NewVBox(
        NewAdaptiveGridWithRatios([]float32{0.1, 0.4, 0.4, 0.1}, buttons[0], buttons[1], buttons[2], buttons[3]),
        NewAdaptiveGridWithRatios([]float32{0.2, 0.3, 0.1, 0.4}, buttons[4], buttons[5], buttons[6], buttons[7]),
        NewAdaptiveGridWithRatios([]float32{0.6, 0.1, 0.2, 0.1}, buttons[8], buttons[9], buttons[10], buttons[11]),
        NewAdaptiveGridWithRatios([]float32{0.1, 0.4, 0.4, 0.1}, buttons[12], buttons[13], buttons[14], buttons[15]),
        NewAdaptiveGridWithRatios([]float32{0.1, 0.9}, pgBar, progressBar),
    ))

    myWindow.ShowAndRun()
}

我以不同的比例添加了多个按钮和其他小部件。

以上就是自定义 Fyne 自适应网格布局的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: 自定义 Fyne 自适应网格布局

本文链接: https://www.lsjlt.com/news/563835.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 自定义 Fyne 自适应网格布局
    问题内容 我正在修改fyne库的container.newadaptivegrid(),以便根据我们传递的比例切片渲染小部件的宽度。截至目前,container.newadaptive...
    99+
    2024-02-12
  • HTML教程:如何使用Grid布局进行自适应网格自动布局
    HTML教程:如何使用Grid布局进行自适应网格自动布局,需要具体代码示例导语在Web开发中,网格布局(Grid layout)是一种更为灵活和强大的布局系统。它允许开发者将页面划分为网格单元,并通过定义行列的数量和大小来控制元素在这些单元...
    99+
    2023-10-26
    html 布局 Grid
  • HTML教程:如何使用Grid布局进行栅格自适应网格布局
    HTML教程:如何使用Grid布局进行栅格自适应网格布局,需要具体代码示例引言:随着互联网的发展,网页布局变得越来越重要。传统的网页布局方法,如使用表格或浮动布局,往往需要大量的代码和调整来实现自适应的效果。而CSS3中引入的Grid布局则...
    99+
    2023-10-27
    html 关键词: Grid布局 栅格自适应
  • HTML教程:如何使用Grid布局进行自适应网格布局
    HTML教程: 如何使用Grid布局进行自适应网格布局在前端开发中,网页布局是一个重要的环节。而在现代的网页布局中,Grid布局已经成为了一种非常流行的选择。它可以帮助我们快速、灵活地构建各种网格布局,并且能够实现自适应的效果。本篇文章将介...
    99+
    2023-10-27
    HTML教程:Grid布局
  • HTML教程:如何使用Grid布局进行自适应网格项布局
    在现代的网页设计中,自适应布局是至关重要的。通过自适应布局,网页可以在不同的设备和屏幕上呈现出最佳的显示效果,提供更好的用户体验。在这方面,CSS Grid布局是一种强大的工具,可以帮助我们实现网页布局的自适应性。本文将介绍如何使用Grid...
    99+
    2023-10-21
    自适应 html Grid布局
  • HTML教程:如何使用Grid布局进行栅格自适应布局
    引言:在现代Web设计中,页面布局的自适应性是一个重要的考虑因素。传统的布局方法(如浮动和定位)虽然可以实现一定程度的自适应,但往往需要大量的代码和调整。而CSS Grid布局提供了一种简单而强大的方式来实现栅格自适应布局。本教程将详细介绍...
    99+
    2023-10-21
    html Grid布局 栅格布局
  • flex布局如何实现每行固定数量+自适应布局
    这篇文章主要介绍flex布局如何实现每行固定数量+自适应布局,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体如下:效果展示解析 <div class="template"...
    99+
    2023-06-08
  • 自适应布局的处理方法
    本篇内容介绍了“自适应布局的处理方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!左边固定,右边自适应(圣杯布局的实现): 代码如下:<...
    99+
    2023-06-08
  • vue自适应布局postcss-px2rem详解
    首先,我们来了解一下lib-flexible和amfe-flexible:lib-flexible是淘宝项目组开发出来的一个开源插件,会自动在html的head中添加一个meta n...
    99+
    2024-04-02
  • HTML教程:如何使用Grid布局进行栅格自动适应布局
    HTML教程:如何使用Grid布局进行栅格自动适应布局在现代网页设计中,栅格布局(Grid Layout)成为了一种流行的布局方式。它可以让网页的元素在网格系统中进行自动适应布局,使得页面在不同屏幕尺寸上都能够展现出良好的排版效果。在本篇文...
    99+
    2023-10-27
    html Grid布局 栅格适应布局
  • css怎么设置自适应布局
    非常抱歉,由于您没有提供文章标题,我无法为您生成一篇高质量的文章。请您提供文章标题,我将尽快为您生成一篇优质的文章。...
    99+
    2024-05-21
  • css3自适应布局如何实现
    这篇文章主要介绍“css3自适应布局如何实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“css3自适应布局如何实现”文章能帮助大家解决问题。 ...
    99+
    2024-04-02
  • CSS自定义属性+CSS Grid网格实现超级的布局能力
    最近我还注意到的一件事就是CSS自定义属性。CSS自定义属性的工作方式有点像SASS和其他预处理器中的变量,主要的区别在于其它方法都是在浏览器中编译后生成,还是原本的CSS写法。CSS自定义属性是真正的动态变量,可以在样式表中或使用java...
    99+
    2023-06-03
  • html5自适应页面布局的方法
    这篇文章主要讲解了“html5自适应页面布局的方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“html5自适应页面布局的方法”吧!   一、静态布局(S...
    99+
    2024-04-02
  • css3多兰自适应布局的方法
    这篇“css3多兰自适应布局的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“css3多...
    99+
    2024-04-02
  • js如何自定义瀑布流布局插件
    这篇文章主要介绍js如何自定义瀑布流布局插件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!瀑布流布局是网页中经常采用的一种布局方式,其布局有如下特点:瀑布流布局特点: (1)图文元素...
    99+
    2024-04-02
  • css怎么实现中间自适应布局
    本篇文章为大家展示了css怎么实现中间自适应布局,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。问题:如何实现三栏布局(高度固定,左中右的结构)假设高度已知,请写出三栏布局,其中左右宽度均为300px...
    99+
    2023-06-08
  • HTML教程:如何使用Grid布局进行自适应布局
    在现代的网页设计中,自适应布局是至关重要的,因为它可以确保网页在不同设备和屏幕尺寸上都能展示出最佳的效果。而CSS Grid布局则是一种强大的工具,可以实现灵活且响应式的布局效果。本文将介绍如何使用Grid布局进行自适应布局,同时提供具体的...
    99+
    2023-10-21
    html 自适应布局 Grid布局
  • html左中右自适应布局的方法
    本篇内容介绍了“html左中右自适应布局的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在最新的html标准中 有了个calc css表...
    99+
    2023-06-08
  • 使用Rem怎么实现自适应布局
    本篇文章给大家分享的是有关使用Rem怎么实现自适应布局,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。在移动端我们一般会设置布局视口宽度=设备宽度,即内容呈现的区域在设备屏幕内。...
    99+
    2023-06-08
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作