KivyMD MDList 移除读取widgets后间距布局问题

KivyMD MDList spacing layout problem after removing and readding widgets

我正在使用 KivyMD 构建应用程序。我有一个用 MDLabels 制作列表的函数。列表初始化后,我想 'refresh' 包含最新数据的列表。因此我使用 clear_widgets() 从列表中删除标签,然后用新数据重建列表。

问题是在重建列表后,间距似乎会被打乱。我试过很多东西,但我无法弄清楚这种情况下问题出在哪里。

有人知道这里出了什么问题吗?我在下面复制了一个简单的例子。 非常感谢!

kivy版本=2.0.0, kivymd 版本 = 0.104.1, python 3.7

main.py

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel

class MainApp(MDApp):
    def build(self):
        screen = Builder.load_file('kivy.kv')

        return screen

    def make_list(self):
        self.root.ids.button.text = 'Click me again'
        if self.root.ids.list.children:
            self.root.ids.list.clear_widgets()

        if not self.root.ids.list.children:
            for i in range(9):
                self.root.ids.list.add_widget(MDLabel(text='Label' + str(i)))


MainApp().run()

.kv 文件

Screen:
    BoxLayout:
        orientation: 'horizontal'

        FloatLayout:

            MDLabel:
                text: 'Click the button below for a list'
                halign: 'center'
                pos_hint: {'center_x': .5, 'center_y': .6}

            MDFlatButton:
                id: button
                text: 'button'
                pos_hint: {'center_x': .5, 'center_y': .4}
                on_release: app.make_list()

        FloatLayout:
            MDList:
                id: list
                spacing: 40
                pos_hint: {'center_x': .7, 'center_y': .6}

很难理解 MDListMDLabel 是如何计算它们的高度的,但是您可以通过指定每个 MDLabelheight 来解决您的问题。一种方式是这样的:

self.root.ids.list.add_widget(MDLabel(text='Label' + str(i), size_hint_y=None, height=15))