如何在 Kivy 中使用 ScrollView?

How to use the ScrollView in Kivy?

我想在屏幕的特定部分创建滚动视图。
视图中的内容数量可以更改 - 因此我想通过 for 循环将小部件插入到视图中。

This is how the view is going to be
我在这个问题上苦苦挣扎了一段时间。 我似乎不能:

  1. 使用 for 循环将小部件插入视图
  2. 将视图放在特定位置。

`

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.

我想你所缺少的只是你必须设置你添加到 GridLayout 的每个 Label 的高度,你还必须设置 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)

另一种方法是使用kivy语言设置ScrollView。此方法利用 GridLayout 计算的 minimum_height,因此您不必计算 GridLayout:

height
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()