如何在Kivy ScrollView中滚动GridLayout?

发布于 2021-01-29 14:11:06

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

关注者
0
被浏览
134
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    根据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'))
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看