[英]Scroll contents of GridLayout in ScrollView - Kivy
我首先要說的是,我已經嘗試了網絡上涉及kv lang的每個示例。 我一次都沒有成功。
這個想法很簡單:當我向上/向下/滾動時, ScrollView()
中GridLayout()
的內容將向上或向下滾動。 我能做的最好的事情就是在運行程序時使滾動條淡入視圖。 不幸的是無法滾動。
<Root>
grid_layout: grid_layout
ScreenManager:
...
Screen:
...
ScrollView:
GridLayout:
id: grid_layout
size_hint_y: None
cols: 1
height: self.minimum_height
<list of buttons>
在根類(RelativeLayout)的__init__
方法中綁定minimum_height
:
grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))
我遵循了https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py將其轉換為kv lang-滾動條可見,無法滾動。 還嘗試了Google網上論壇中與使用kv lang有關的每個示例。 仍然沒有滾動:\\
由於未知原因,使用buildozer編譯並在Android上運行失敗。
我將不勝感激,可以提供任何幫助。
這個:
height: self.minimum_height
應該:
minimum_height: self.height
這是不必要的:
grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))
除非內容大於滾動視圖的高度,否則它也不會滾動:
完整代碼:
from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
Builder.load_string('''
<Root>:
ScrollView:
size_hint: 1, .1
# setting the width of the scrollbar to 50pixels
bar_width: 50
# setting the color of the active bar using rgba
bar_color: 5, 10, 15, .8
# setting the color of the inactive bar using rgba
bar_inactive_color: 5, 20, 10, .5
# setting the content only to scroll via bar, not content
scroll_type: ['bars']
GridLayout:
size_hint_y: None
cols: 1
minimum_height: self.height
Button
text: 'one'
Button:
text: 'two'
Button:
text: 'three'
Button:
text: 'four'
''')
class Root(FloatLayout):
pass
class DemoApp(App):
def build(self):
return Root()
if __name__ == '__main__':
DemoApp().run()
無法滾動是由於對Kivy的touch
處理程序的誤解。 完全與我的問題中提到的代碼無關。
關鍵是使GridLayout
大於ScrollView
,因此可以在ScrollView
平移GridLayout
。
對於僅在使用kvlang的ScreenManager
內部使用ScrollView
ScreenManager
:
ScreenManager:
id: screen_manager
Screen:
manager: screen_manager
id: main_screen
name: 'main'
ScrollView:
bar_width: 4
# pos_hint defaults to 1,1 so no need to declare it
GridLayout:
size_hint_y: None
cols: 1
# you do not need to manually bind to setter('height') in
# python - perfectly possible with kv lang
# this allows for height to update depending on the
# collective heights of its child widgets
height: self.minimum_height
<----- widgets here ----->
# for scroll to show/work there must be more widgets
# then can fit root.height. If not there is no need
# for scrollview :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.