我有此代码想要更改按钮的位置,但是如果我更改位置,则滚动将不再起作用。如何设法使其运作?以下是工作版本。如果我将size_hint:1,.1更改为size_hint:1,.7,则滚动不再起作用...
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.floatlayout import FloatLayout
Builder.load_string('''
<Root>:
ScrollView:
size_hint: 1, .1
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()
您做对了。 ScrollView
允许您滚动查看GridLayout
中不适合ScrollView
的部分。将size_hint
设置为(1, .7)
时,所有内容都适合ScrollView
,因此它不会滚动。
您可以通过添加Widgets
来占据更多空间来强制滚动(例如Labels
无文本):
<Root>:
ScrollView:
size_hint: 1, .7
GridLayout:
size_hint_y: None
cols: 1
height: self.minimum_height
Label:
text: ''
size_hint_y: None
height: 300
Button
text: 'one'
size_hint: 1, None
height: self.texture_size[1]
Button:
text: 'two'
size_hint: 1, None
height: self.texture_size[1]
Button:
text: 'three'
size_hint: 1, None
height: self.texture_size[1]
Button:
text: 'four'
size_hint: 1, None
height: self.texture_size[1]
Label:
text: ''
size_hint_y: None
height: 300