如何在Kivy ScrollView中滚动GridLayout?

2024-04-29 14:25:24 发布

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

目前这是我的kv代码,无法滚动:

BoxLayout:
    id: bl
    orientation: 'vertical'
    padding: 10, 10
    row_default_height: '48dp'
    row_force_default: True
    spacing: 10, 10

    GridLayout:
        id: layout_content
        cols: 1
        row_default_height: '20dp'
        row_force_default: True
        spacing: 0, 0
        padding: 0, 0

        Label:
            text: 'You don''t have any downloads. Please add new download from Home screen'

如何使上面的kv代码可滚动?我知道Kivy ScrollView只接受一个子视图,而且我已经将GridLayout设置为一个新的ScrollView的子视图。但没用。有什么建议吗?


Tags: 代码视图idtruedefaultrowheightpadding
1条回答
网友
1楼 · 发布于 2024-04-29 14:25:24

根据documentation for ScrollView提示,必须至少禁用ScrollView的一个子大小:

<Controller>:
    layout_content: layout_content
    BoxLayout:
        id: bl
        orientation: 'vertical'
        padding: 10, 10
        row_default_height: '48dp'
        row_force_default: True
        spacing: 10, 10
        ScrollView:
            size: self.size
            GridLayout:
                id: layout_content
                size_hint_y: None
                cols: 1
                row_default_height: '20dp'
                row_force_default: True
                spacing: 0, 0
                padding: 0, 0

                Label:
                    text: "Lorem ipsum dolor sit amet"

并绑定布局的大小以适应自身:

# main.py

class Controller(FloatLayout):
    layout_content=ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Controller, self).__init__(**kwargs)
        self.layout_content.bind(minimum_height=self.layout_content.setter('height'))

相关问题 更多 >