【发布时间】:2019-08-14 03:39:32
【问题描述】:
我正在尝试在我的用户界面中使用滚动条。我的用户界面将能够从用户输入的文本中延伸出来。
但它的伸展方式不会导致 Scrollview “激活”。
此代码只是在 4 秒后增加小部件的大小来测试它。
首先它看起来像
然后看起来像
请注意,我们现在看不到最后一个列表项。但是滚动条应该在屏幕右侧可见,表明我们可以向下滚动查看它。但我们不能。 Scrollview 不知道内容已超出屏幕底部。
守则
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from lib.modules.adaptive_grid_layout import Adaptive_GridLayout
#This should have not enough content to scroll at first,
#but the size change should push some content past the border
Builder.load_string('''
<GrowingLabel>:
padding: 10, 5
size_hint_y: None
text_size: self.width, None
group: 'test'
canvas.before:
Color:
rgba: .7, .7, .7, 1
Rectangle:
pos: self.pos
size: self.size
<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
spacing: 0, 0
padding: 0, 0
Adaptive_GridLayout:
id: Row2
cols: 1
grow_rows: True
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
GrowingLabel:
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dkdsjahf lkasjkat"
Label:
height: 20
text: "Lorem ipsdodo dod dodo do dodt"
Label:
height: 20
text: "Lorem ipsdkjwww ww woij ksdsdf sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Lorem ipsum dolor sit amet"
Label:
height: 20
text: "Last List item"
''')
class GrowingLabel(Label):
def __init__(self, **kwargs):
super(GrowingLabel, self).__init__(**kwargs)
#self.size_hint_y = None
self.height = 20
Clock.schedule_once(lambda dt: self.changeHeight(120), timeout=4)
def changeHeight(self, p_val):
self.height = p_val
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'))
class Nested2App(App):
def build(self):
return Controller()
if __name__ == '__main__':
Nested2App().run()
注意:我正在使用一个名为 Adaptive_GridLayout 的自定义布局来处理缩放问题,您可以找到 here。
我的问题
有没有办法手动触发滚动视图中滚动条的显示?或者有什么方法可以刷新滚动视图,以便它注意到它的内容有多大并做出适当的响应?
【问题讨论】:
-
不熟悉
Adaptive_GridLayout,但请尝试将size_hint: 1.0, None和height: self.minimum_height添加到您的Adaptive_GridLayout规则中。 -
如果你想尝试一下,我在帖子中添加了指向 Adaptive_GridLayout 的链接。原 Adaptive_GridLayout 可以在here找到。
标签: python python-3.x kivy