Kivy 在 BoxLayout 中左对齐 Switch
Kivy align Switch left in BoxLayout
我想将开关对齐到 BoxLayout 的左侧。对于标签,我使用以下代码实现了这一点:
text_size: self.size
这会将我的标签文本放在我的框布局的左下角。但是,我无法使用 switch 小部件 来做同样的事情。我尝试使用 size_hint_x、大小、位置等,但我无法在不影响框的大小的情况下正确对齐元素。
目前我的标签对齐正确,所以我尝试为它们分配 ID 并根据标签的当前位置定位开关,如下所示:
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
text_size: self.size
valign: 'middle'
Label:
text: 'this is already correctly aligned'
id: 'labelCorrectlyAligned'
#Some other code
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
#here i need something like text_size: self.size but for switches
Switch:
size_hint_x: labelCorrectlyAligned.pos[0] #this should be the current X-position of the label
#pos_hint_x: labelCorrectlyAligned.pos[0] #didnt work either
如果我正确理解你的问题,你可以将 Switch
的 size
设置为最小值,如 documentation:
中所述
The minimum size required is 83x32 pixels
任何比该尺寸大的尺寸都会使 83 x 32 图像以该较大尺寸为中心。
此外,在这些布局情况下,我发现为 Widgets
的背景着色有助于轻松准确地查看它们的确切位置和大小。这是执行上述两个建议的“kv”的修改版本:
BoxLayout:
orientation: 'vertical'
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
canvas.before:
Color:
rgba: 1,0,0,1
Rectangle:
pos: self.pos
size: self.size
Label:
text: 'this is already correctly aligned'
text_size: self.size
valign: 'middle'
id: 'labelCorrectlyAligned'
canvas.before:
Color:
rgba: 0,1,0,1
Rectangle:
pos: self.pos
size: self.size
# Some other code
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
canvas.before:
Color:
rgba: 1,1,0,1
Rectangle:
pos: self.pos
size: self.size
Switch:
size_hint: None, None
size: 83, 32
on_active: app.setBluetoothConnection(self)
canvas.before:
Color:
rgba: 0,0,1,1
Rectangle:
pos: self.pos
size: self.size
当然,当您对布局感到满意时,只需删除 canvas.before:
块即可。
顺便说一句,text_size
和valign
在BoxLayout
中没有作用,它们必须在Label
规则中。
因此,Switch
和 Label
都位于 BoxLayout
的左侧。调整 Label
大小的常用方法是使用:
size_hint: None, None
size: self.texture_size
这为您提供了 Label
的最小尺寸。
编辑:为方便起见,我为我的左对齐开关创建了一个动态 class:
<SwitchL@Switch>:
size_hint_x: None
size: 83, self.height
我想将开关对齐到 BoxLayout 的左侧。对于标签,我使用以下代码实现了这一点:
text_size: self.size
这会将我的标签文本放在我的框布局的左下角。但是,我无法使用 switch 小部件 来做同样的事情。我尝试使用 size_hint_x、大小、位置等,但我无法在不影响框的大小的情况下正确对齐元素。 目前我的标签对齐正确,所以我尝试为它们分配 ID 并根据标签的当前位置定位开关,如下所示:
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
text_size: self.size
valign: 'middle'
Label:
text: 'this is already correctly aligned'
id: 'labelCorrectlyAligned'
#Some other code
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
#here i need something like text_size: self.size but for switches
Switch:
size_hint_x: labelCorrectlyAligned.pos[0] #this should be the current X-position of the label
#pos_hint_x: labelCorrectlyAligned.pos[0] #didnt work either
如果我正确理解你的问题,你可以将 Switch
的 size
设置为最小值,如 documentation:
The minimum size required is 83x32 pixels
任何比该尺寸大的尺寸都会使 83 x 32 图像以该较大尺寸为中心。
此外,在这些布局情况下,我发现为 Widgets
的背景着色有助于轻松准确地查看它们的确切位置和大小。这是执行上述两个建议的“kv”的修改版本:
BoxLayout:
orientation: 'vertical'
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
canvas.before:
Color:
rgba: 1,0,0,1
Rectangle:
pos: self.pos
size: self.size
Label:
text: 'this is already correctly aligned'
text_size: self.size
valign: 'middle'
id: 'labelCorrectlyAligned'
canvas.before:
Color:
rgba: 0,1,0,1
Rectangle:
pos: self.pos
size: self.size
# Some other code
BoxLayout:
padding: 100, 0, 0, 0
orientation: 'horizontal'
canvas.before:
Color:
rgba: 1,1,0,1
Rectangle:
pos: self.pos
size: self.size
Switch:
size_hint: None, None
size: 83, 32
on_active: app.setBluetoothConnection(self)
canvas.before:
Color:
rgba: 0,0,1,1
Rectangle:
pos: self.pos
size: self.size
当然,当您对布局感到满意时,只需删除 canvas.before:
块即可。
顺便说一句,text_size
和valign
在BoxLayout
中没有作用,它们必须在Label
规则中。
因此,Switch
和 Label
都位于 BoxLayout
的左侧。调整 Label
大小的常用方法是使用:
size_hint: None, None
size: self.texture_size
这为您提供了 Label
的最小尺寸。
编辑:为方便起见,我为我的左对齐开关创建了一个动态 class:
<SwitchL@Switch>:
size_hint_x: None
size: 83, self.height