我有一个布局为4行2列的网格:
import QtQuick 2.9
import QtQuick.Controls 2.2
Item {
id: page
Grid {
property int buttonSize: height * 0.2
property int spacingSize: height * 0.04
id: grid
height: parent.height
spacing: spacingSize
anchors.horizontalCenter: parent.horizontalCenter
topPadding: spacingSize
flow: Grid.TopToBottom
columns: 2
rows: 4
Rectangle {
color: "blue"
height: grid.buttonSize
width: grid.buttonSize
visible: true //this comes from c++, not always true
}
Rectangle {
color: "blue"
height: grid.buttonSize
width: grid.buttonSize
visible: true //this comes from c++, not always true
}
Rectangle {
color: "blue"
height: grid.buttonSize
width: grid.buttonSize
visible: true //this comes from c++, not always true
}
Rectangle {
color: "blue"
height: grid.buttonSize
width: grid.buttonSize
visible: true //this comes from c++, not always true
}
Rectangle {
color: "blue"
height: grid.buttonSize
width: grid.buttonSize
visible: true //this comes from c++, not always true
}
}
}
如果我有5+可见元素(绘制2列),一切工作正常,但如果我有4个或更少的可见项目(绘制1列),网格保持列间距可见,元素不居中。
我该如何解决这个问题?谢谢你
解决方案很简单,只需删除列的数量。这将使网格只在必要时创建列。