目前这是我的kv代码,不能滚动:
BoxLayout:
id: bl
orientation: 'vertical'
padding: 10, 10
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
GridLayout:
id: layout_content
cols: 1
row_default_height: '20dp'
row_force_default: True
spacing: 0, 0
padding: 0, 0
Label:
text: 'You don''t have any downloads. Please add new download from Home screen'
如何使上述kv代码可滚动?我知道Kivy ScrollView只接受一个孩子,我已经让GridLayout成为新ScrollView的孩子。但它不起作用。有什么建议吗?
答案 0 :(得分:12)
根据documentation for ScrollView你必须至少禁用一个ScrollView的子size_hint:
<Controller>:
layout_content: layout_content
BoxLayout:
id: bl
orientation: 'vertical'
padding: 10, 10
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
ScrollView:
size: self.size
GridLayout:
id: layout_content
size_hint_y: None
cols: 1
row_default_height: '20dp'
row_force_default: True
spacing: 0, 0
padding: 0, 0
Label:
text: "Lorem ipsum dolor sit amet"
并绑定布局的大小以适应自己:
# main.py
class Controller(FloatLayout):
layout_content=ObjectProperty(None)
def __init__(self, **kwargs):
super(Controller, self).__init__(**kwargs)
self.layout_content.bind(minimum_height=self.layout_content.setter('height'))
答案 1 :(得分:0)
工作并修复了滚动视图最大化时不随窗口调整长度的问题。没有需要的人就没有kp
kivy.require('1.11.1')
class ScrollTest(ScrollView):
def __init__(self,**kwargs):
super().__init__(**kwargs)
Window.bind(on_maximize=self.testing)
self.size_hint = (1,None)
self.pos_hint = {'center_x':0.5,'top': 1}
self.size = (Window.width,Window.height)
self.inside = GridLayout()
self.inside.cols = 1
self.inside.size_hint_y= None
self.inside.spacing = 10
self.inside.bind(minimum_height=self.inside.setter('height'))#checks when window maximized
for i in range(1,5):
self.submit = Button(text='something',size_hint_y=None, height=40)
self.submit.bind(on_press=self.wPressed)
self.inside.add_widget(self.submit)
self.add_widget(self.inside)
def wPressed(self,instance):
self.submit = Button(text='something',size_hint_y=None, height=40)
self.submit.bind(on_press=self.wPressed)
self.inside.add_widget(self.submit)
def testing(self,instance):
self.size= (Window.width,Window.height)
class MyApp(App):
def build(self):
self.screen_manager = ScreenManager()
'''Creation of login screen'''
self.login_page = ScrollTest()
screen = Screen(name= 'Login')
screen.add_widget(self.login_page)
self.screen_manager.add_widget(screen)
return self.screen_manager
if __name__ == '__main__':
MyApp().run()