Kivys GridLayout中的Scrollview

时间:2017-07-17 00:56:14

标签: python kivy kivy-language

这是我的应用程序的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

问题是我的输出看起来像这样(左下角)而我希望根据设备的大小逐行排列 enter image description here

1 个答案:

答案 0 :(得分:1)

如果您希望ScrollView的内容占用所有可用空间,则无法执行此操作:

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

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

请记住,如果您不指定cols或rows,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()

输出:

enter image description here