我不能在Kivy中心对齐浮动布局

2024-04-16 19:07:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在中间对齐浮动布局,帮我吗? 上传图片以供理解。。。 我的想法是在Kivy中重新创建布局windows8retro,添加图标windows8retro in按钮。在

enter image description here

我的kv文件:

<SMAgro>:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        FloatLayout:
            canvas:
                Color:
                    rgb: [1,1,1,1]
                Rectangle:
                    pos: self.pos
                    size: self.size
            Button:
                background_color: [.4,1,.1,1]
                pos_hint: {'x': 0.26, 'y': 0.328571}
                size_hint: 0.45, 0.3
                text: "Equipamentos"
#                StackLayout:
#                    pos: self.parent.pos
#                    size: self.parent.size
#                    orientation: 'lr-tb'
#                    Image:
#                        source: 'kivy.png'
#                        size_hint_x: None
#                        width: 74
#                    Label:
#                        size_hint_x: None
#                        width: 100
#                        text: "Equipamentos"

            Button:
                background_color: [.4,1,.1,1]
                pos_hint: {'x': 0.26, 'y': 0.15}
                size_hint: 0.225, 0.18
                text: 'Configuracoes'
            Button:
                background_color: [.4,1,.1,1]
                pos_hint: {'x': 0.486, 'y': 0.15}
                size_hint: 0.225, 0.18
                text: 'Sobre'

Tags: textposselfnonesizebutton布局color
1条回答
网友
1楼 · 发布于 2024-04-16 19:07:18

您可以设置FloatLayout的大小并添加pos_hint: {'center_y': 0.5, 'center_x': 0.5},因此例如:最宽的按钮的大小为0.45,按钮的大小为0.18+0.3。在

容器大小:size_hint: 0.45, 0.48

现在,如果按钮有size_hint: 1, 1,它是父容器(FloatLayout)的100%宽度和高度,等于窗口的45%宽度和48%的高度。在

此代码显示居中浮动布局

FloatLayout:
    pos_hint: {'center_y': 0.5, 'center_x': 0.5}
    size_hint: 0.45, 0.48
    canvas:
        Color:
            rgb: [1,1,1,1]
        Rectangle:
            pos: self.pos
            size: self.size
    Button:
        background_color: [.4,1,.1,1]
        pos_hint: {'x': 0, 'y': 0.375}
        size_hint: 1, 0.625
        text: "Equipamentos"
    Button:
        background_color: [.4,1,.1,1]
        pos_hint: {'x': 0, 'y': 0}
        size_hint: .5, 0.375
        text: 'Configuracoes'
    Button:
        background_color: [.4,1,.1,1]
        pos_hint: {'x': .5, 'y': 0}
        size_hint: .5, 0.375
        text: 'Sobre'

centered FloatLayout

相关问题 更多 >