如何在KivyMD的MDlist项上放置MDDropdownMenu?

时间:2020-11-09 18:29:02

标签: kivy kivy-language kivymd

我有一个ScrollView,下面有MDList,我希望“当我单击MDlist时,MDDropdownMenu项应恰好在该项上打开”。

ScrollView:
    pos_hint: {'center_x': .5, 'center_y': .4}
    MDList:
        pos_hint: {'center_x': .5, 'center_y': .85}
        id: box

1 个答案:

答案 0 :(得分:1)

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.list import OneLineListItem
from kivymd.uix.menu import MDDropdownMenu

KV = """
ScrollView:

    MDList:
        id: box
        cols: 1
"""


class Test(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def show_menu(self, instance):
        menu_items = [{"text": f"Item {i}"} for i in range(5)]
        menu = MDDropdownMenu(caller=instance, items=menu_items, width_mult=4
        )
        menu.open()

    def on_start(self):
        for i in range(20):
            self.root.ids.box.add_widget(
                OneLineListItem(text=f"Item {i}", on_release=self.show_menu)
            )


Test().run()