我想在屏幕的特定部分创建滚动视图。
视图中将包含的内容数量可以更改-为此,我想通过for循环将小部件插入视图。
This is how the view is going to be
我为此问题苦了一段时间。
我似乎无法:
`
class PlaylistWidget(ScrollView):
def __init__(self, **kwargs):
super(PlaylistWidget, self).__init__(**kwargs)
self.bar_width = 50
self.size_hint = 1, .1
self.scroll_type = ['bars']
self.bar_inactive_color = 5, 20, 10, .5
self.bar_color = 5, 10, 15, .8
self.do_scroll_x = False
self.do_scroll_y = True
grid = GridLayout()
grid.height = self.size[1]
grid.size_hint_y = None
grid.cols = 1
grid.row_default_height = '20dp'
grid.row_force_default = True
for i in range(148):
a = Label()
a.text = "Blah blah blah"
a.font_size = 30
a.size_hint_y = None
a.size_hint_x = 1.0
a.height = self.size[1]
grid.add_widget(a)
self.add_widget(grid)
` 这是我做过的尝试代码....
谢谢。
Ofek Harel。
答案 0 :(得分:0)
我想您所缺少的就是必须设置添加到Label
的每个GridLayout
的高度,并且还必须设置GridLayout
的高度。这是执行此操作的代码的修改版本:
class PlaylistWidget(ScrollView):
def __init__(self, **kwargs):
super(PlaylistWidget, self).__init__(**kwargs)
self.bar_width = 50
self.size_hint = 1, .1
self.scroll_type = ['bars']
self.bar_inactive_color = 5, 20, 10, .5
self.bar_color = 5, 10, 15, .8
self.do_scroll_x = False
self.do_scroll_y = True
grid = GridLayout()
# star with zero height
grid.height = 0
grid.size_hint_y = None
grid.cols = 1
grid.row_default_height = '20dp'
grid.row_force_default = True
for i in range(148):
a = Label()
a.text = "Blah blah blah"
a.font_size = 30
a.size_hint_y = None
a.size_hint_x = 1.0
# set label height
a.height = grid.row_default_height
# increment grid height
grid.height += a.height
grid.add_widget(a)
self.add_widget(grid)
另一种方法是使用猕猴桃语言设置ScrollView
。这种方法利用了minimum_height
计算出的GridLayout
的优势,因此您不必计算height
中的GridLayout
:
class PlaylistWidget(ScrollView):
def add_labels(self, *args):
grid = self.ids.grid
for i in range(148):
a = Label()
a.text = "Blah blah blah"
a.font_size = 30
a.size_hint_y = None
a.size_hint_x = 1.0
# set label height
a.height = grid.row_default_height
grid.add_widget(a)
kv = '''
PlaylistWidget:
bar_width: 50
size_hint: 1, .1
scroll_type: ['bars']
bar_inactive_color: 5, 20, 10, .5
bar_color: 5, 10, 15, .8
do_scroll_x: False
do_scroll_y: True
GridLayout:
id: grid
cols: 1
row_default_height: '20dp'
row_force_default: True
size_hint_y: None
height: self.minimum_height
'''
class TestApp(App):
def build(self):
pl = Builder.load_string(kv)
Clock.schedule_once(pl.add_labels)
return pl
TestApp().run()