目标:可以。 只需在屏幕的右上方有两个并排的按钮。
尝试的方法。 使用AnchorLayout在屏幕右上方放置一个BoxLayout。
.kv文件。
AnchorLayout:
anchor_x: "right"
anchor_y: "top"
BoxLayout:
orientation: 'horizontal'
Button:
text: "Button 1"
size_hint: None, None
size: 100, 100
Button:
text: "Button 2"
size_hint: None, None
size: 100,100
结果: 这将按钮放置在屏幕的左下角,而不是预期的右上角。
问题:这有什么问题吗? 这有什么问题,什么是将一排按钮固定在屏幕右上方的最佳方法?
出现这种情况是因为 BoxLayout
被设置为与 AnchorLayout
由于默认的 size_hint
的 (1,1)
. 你可以通过添加 size_hint: None, None
和 size: self.minimum_size
:
AnchorLayout:
anchor_x: "right"
anchor_y: "top"
BoxLayout:
orientation: 'horizontal'
size_hint: None, None
size: self.minimum_size
Button:
text: "Button 1"
size_hint: None, None
size: 100, 100
Button:
text: "Button 2"
size_hint: None, None
size: 100,100