Kivy: 你能在AnchorLayout中使用BoxLayout吗?

问题描述 投票:0回答:1

目标:可以。 只需在屏幕的右上方有两个并排的按钮。

尝试的方法。 使用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

结果: 这将按钮放置在屏幕的左下角,而不是预期的右上角。

问题:这有什么问题吗? 这有什么问题,什么是将一排按钮固定在屏幕右上方的最佳方法?

kivy kivy-language
1个回答
0
投票

出现这种情况是因为 BoxLayout 被设置为与 AnchorLayout由于默认的 size_hint(1,1). 你可以通过添加 size_hint: None, Nonesize: 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
© www.soinside.com 2019 - 2024. All rights reserved.