QML使用anchors(锚)对元素进行布局。anchoring(锚定)是基础元素对象的基本属性,可以被所有的可视化QML元素使用。一个anchors(锚)就像一个协议,并且比几何变化更加强大。Anchors(锚)是相对关系的表达式,你通常需要与其它元素搭配使用。
一个元素有6条锚定线(top顶,bottom底,left左,right右,horizontalCenter水平中,verticalCenter垂直中)。在文本元素(Text Element)中有一条文本的锚定基线(baseline)。每一条锚定线都有一个偏移(offset)值,在top(顶),bottom(底),left(左),right(右)的锚定线中它们也被称作边距。对于horizontalCenter(水平中)与verticalCenter(垂直中)与baseline(文本基线)中被称作偏移值。

1. 组件
// Color.qmlimport QtQuick 2.15Rectangle {width: 100height: 100border.color: Qt.lighter(color)}
2. 布局
2.1 元素填充它的父元素
// 元素填充它的父元素Color {color: "#ff0066"Color {width: 50anchors.fill: parentanchors.margins: 8color: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(1)")}}}
2.2 元素左对齐它的父元素
Color {color: "#ff0066"Color {width: 50; height: 50anchors.left: parent.leftcolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(2)")}}}
2.3 元素的左边与它父元素的右边对齐
// 元素的左边与它父元素的右边对齐Color {color: "#ff0066"Color {width: 50; height: 50anchors.left: parent.rightcolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(3)")}}}
2.4 元素中间对齐。
Blue1与它的父元素水平中间对齐。Blue2与Blue1中间对齐,并且它的顶部对齐Blue1的底部。
// 元素中间对齐Color {color: "#ff0066"Color {id: b11width: 30; height: 30y: 8color: "#ccff66"anchors.horizontalCenter: parent.horizontalCenter}Color {id: b12y:40width: 50; height: 30color: "#ccff66"anchors.topMargin: 4anchors.horizontalCenter: b11.horizontalCenterText {anchors.centerIn: parenttext: qsTr("(4)")}}}
2.5 元素在它的父元素中居中
// 元素在它的父元素中居中Color {color: "#ff0066"Color {width: 50; height: 50anchors.centerIn: parentcolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(5)")}}}
2.6 元素水平居中且设置偏移
元素水平方向居中对齐父元素并向后偏移12像素,垂直方向居中对齐。
// 元素水平方向居中对齐父元素并向后偏移12像素,垂直方向居中对齐Color {color: "#ff0066"Color {width: 50; height: 50anchors.horizontalCenter: parent.horizontalCenteranchors.horizontalCenterOffset: -12anchors.verticalCenter: parent.verticalCentercolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(6)")}}}
3. 完整代码
transformation.qml
import QtQuick 2.15import QtQuick.Window 2.15Rectangle {id: rootwidth: 480height: 300Grid {x: 30;y:30spacing: 30columns: 3// 元素填充它的父元素Color {color: "#ff0066"Color {width: 50anchors.fill: parentanchors.margins: 8color: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(1)")}}}// 元素左对齐它的父元素Color {color: "#ff0066"Color {width: 50; height: 50anchors.left: parent.leftcolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(2)")}}}// 元素的左边与它父元素的右边对齐Color {color: "#ff0066"Color {width: 50; height: 50anchors.left: parent.rightcolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(3)")}}}// 元素中间对齐Color {color: "#ff0066"Color {id: b11width: 30; height: 30y: 8color: "#ccff66"anchors.horizontalCenter: parent.horizontalCenter}Color {id: b12y:40width: 50; height: 30color: "#ccff66"anchors.topMargin: 4anchors.horizontalCenter: b11.horizontalCenterText {anchors.centerIn: parenttext: qsTr("(4)")}}}// 元素在它的父元素中居中Color {color: "#ff0066"Color {width: 50; height: 50anchors.centerIn: parentcolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(5)")}}}// 元素水平方向居中对齐父元素并向后偏移12像素,垂直方向居中对齐Color {color: "#ff0066"Color {width: 50; height: 50anchors.horizontalCenter: parent.horizontalCenteranchors.horizontalCenterOffset: -12anchors.verticalCenter: parent.verticalCentercolor: "#ccff66"Text {anchors.centerIn: parenttext: qsTr("(6)")}}}}}
