一尘不染

如何在Kivy ScrollView中滚动GridLayout?

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的孩子。但这不起作用。有什么建议吗?


阅读 433

收藏
2021-01-20

共1个答案

一尘不染

根据ScrollView文档,您必须至少禁用ScrollView的子size_hint之一:

<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'))
2021-01-20