Kivys GridLayout 中的滚动视图

Scrollview in Kivys GridLayout

这是我的应用程序的 kv 文件

<myAppLayout>       
    canvas.before:
        Color:
            rgba:1, 1, 1,1
        Rectangle:
            pos: self.pos
            size: self.size

    ScrollView:
        size: self.size 
        size_hint: (None, None)
        GridLayout:
            cols:1      

            TextInput:      

                pos_hint:{"center_x":0.5,"y":0.1}
                color:0,0.5,1,1
                background_color:0,0.5,1,1
                size:20,20          


            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size

Python 代码如下所示

class myAppLayout(GridLayout):
    pass

问题是我的输出看起来像这样(左下角),而我想根据设备的大小逐行排列

如果你想让ScrollView的内容占据所有可用的space你不能这样做:

size: self.size 
size_hint: (None, None)

另一方面,ScrollView 中的小部件必须具有定义的高度 (size_hint_y = None),否则它们将自动调整其大小以适应 ScrollView 的大小。

请记住,如果您不指定列数或行数,GridLayout 将抛出异常。您必须为 myAppLayout 分配一定数量的行或列。

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

kv_text = '''

<MyLabel@Label>:
    color:1,0,1,1
    text:"hello"
    text_size:self.size
    size_hint_y:None
    height: 20

<myAppLayout>
    cols: 1
    canvas.before:
        Color:
            rgba:1, 1, 1,1
        Rectangle:
            pos: root.pos
            size: root.size

    ScrollView:
        GridLayout:
            cols:1
            size_hint_y: None 
            height: self.minimum_height   

            TextInput:      
                pos_hint:{"center_x":0.5,"y":0.1}
                color:0,0.5,1,1
                background_color:0,0.5,1,1
                size_hint_y: None
                height: 30          

            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:

'''

class myAppLayout(GridLayout):
    pass

class MyApp(App):
    def build(self):
        return myAppLayout()

def main():
    Builder.load_string(kv_text)
    app = MyApp()
    app.run()

if __name__ == '__main__':
    main()

输出: