如何在 Kivy ScrollView 中滚动 GridLayout?

2022-01-15 00:00:00 python kivy

问题描述

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

At the moment this is my kv code that is not scrollable:

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

How do you make the above kv code scrollable? I know that Kivy ScrollView only accept one child, and I have already make GridLayout to be child of a new ScrollView. But it's not working. Any suggestion?


解决方案

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

According to the documentation for ScrollView you have to disable at least one of the ScrollView's child 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"

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

And bind the layout's size to adapt itself:

# 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'))

相关文章