2017-07-17 81 views
1

這是我的應用程序滾動型在Kivys網格佈局

<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 

的KV文件的問題是我的輸出看起來就像這樣(左下角)的一些事情,而我想根據設備的大小逐行逐行排列 enter image description here

回答

1

如果您希望ScrollView的內容全部佔用能空間,你不能這樣做:

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

在另一方面,滾動型中的小部件必須有一個定義的高度(size_hint_y =無),否則將自動調整其大小,以滾動型的大小。

請記住,如果您不指定列或行,GridLayout將拋出異常。您必須爲myAppLayout分配多個行或列。

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

kv_text = ''' 

<[email protected]>: 
    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

+0

我沒有新的,我是新來kivy,並從網站上嘗試的例子,並感謝您的解釋 – Eka