我正在 QML 中創建一個組件,作業正常,但我希望 TEXT 欄位位于矩形的中心。為此,我已相應地錨定它,但錨定不起作用。我不知道為什么。
Item {
id: root
property int main_rect_height: 32
property int elem_txt_size: 14
property string elem_border_color: "red"
property int elem_width: parent.width/6.5
Rectangle {
id: clients_rect
width: root.parent.width-27
height: main_rect_height
color: "transparent"
border.color: "black"
Rectangle {
id: username_rect
width: elem_width
height: parent.height
color: "transparent"
border.color: elem_border_color
Text {
id: username_txt
text: "USERNAME"
font.pixelSize: elem_txt_size
anchors {
fill: parent
centerIn: parent // THIS DOESN'T WORK!
}
}
}
}
uj5u.com熱心網友回復:
通過使用anchors.fill: parent,您可以設定 Text 物件的大小和位置以匹配父物件。將相同大小的物件居中不會做任何事情,因為它已經占據了父物件的整個空間。默認情況下,文本與頂部和左側對齊。您有兩個選擇:
- 您可以使用 Text 的對齊屬性將其內部的文本對齊以使其居中:
Text {
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
- 或者,您可以簡單地使用
centerIn不帶fill.
Text {
anchors.centerIn: parent
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/395706.html
