我有一个自定义 MDDialog,里面有一个滚动视图。问题是对话框非常小。当我删除滚动视图时,对话框(因为它必须显示的数据量)太大而无法适应屏幕(这就是我添加滚动视图的原因)。如何使对话框更大但又不太大。
运行下面的代码并按带有信息图标的 MDIconButton 时,您将看到 MDDialog。当按下旁边的按钮时 - 程序将崩溃,因为那不是问题所在。
MDDialog 从中获取数据的 2 个目录是
Выпад.txt:
“”” 无
10
Улучшение осанки (снятие боли со спины)
Время
髋屈肌_拉伸.jpg
这是代码:
from os import path, listdir
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import MDDialog, MDDialogIcon, MDDialogHeadlineText, MDDialogButtonContainer, \
MDDialogContentContainer
from kivymd.uix.divider import MDDivider
from kivymd.uix.fitimage import FitImage
from kivymd.uix.label import MDLabel
from kivymd.uix.list import MDListItem
from kivymd.uix.screen import MDScreen
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.scrollview import MDScrollView
exercise_items = [x.replace('.txt', '') for x in listdir('exercises')]
Builder.load_string("""
<CustomOneLineIconListItem>
MDListItemLeadingIcon:
icon: "star-four-points-small"
MDListItemSupportingText:
text: root.text
MDIconButton:
icon: "information"
theme_icon_color: "Custom"
on_press: app.get_running_app().root.get_screen("Fourth").make_info_popup(root)
MDButton:
on_press:
# Basically, this gets the previous_screen variable (which is also a screen) of the FourthScreen, which in
# turn calls its add_exercise function
# Yes, this is an abomination. I know that.
previous_screen_var = app.get_running_app().root.get_screen("Fourth").previous_screen
app.get_running_app().root.get_screen(previous_screen_var).add_exercise(root)
app.change_to_screen(previous_screen_var)
MDButtonIcon:
icon: "plus"
MDButtonText:
text: "Add exercise"
<PreviousExercises>
MDBoxLayout:
orientation: 'vertical'
spacing: dp(10)
padding: dp(20)
MDBoxLayout:
adaptive_height: True
spacing: "5dp"
MDIconButton:
icon: 'arrow-left-bold'
on_press: app.sm.current = 'Third'
MDIconButton:
icon: 'magnify'
MDTextField:
id: search_field
on_text: root.set_list_md_icons(self.text, True)
MDTextFieldHintText:
text: 'Поиск Упражнения'
RecycleView:
id: rv
key_viewclass: 'viewclass'
key_size: 'height'
RecycleBoxLayout:
padding: dp(10)
default_size: None, dp(48)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
""")
class FourthScreen(MDScreen):
previous_screen = None
def __init__(self, **kwargs):
super(FourthScreen, self).__init__(**kwargs)
def make_info_popup(self, widget):
filename = widget.text + ".txt"
file = open(path.join('exercises', filename), 'r', encoding='utf-8')
s_name, rest_time, muscles, exc_type, image, *instructions = file.readlines()
s_name = str(s_name)
muscles = str(muscles)
exc_type = str(exc_type)
lbl_text = [str(s_name), str(exc_type), str(muscles)]
for instruction in instructions:
lbl_text.append(instruction)
for x in lbl_text:
x = x.replace("\n", "")
file.close()
self.dialog = MDDialog(
MDDialogIcon(icon='information'),
MDDialogHeadlineText(text="Информация о упражнении"),
CustomMDDialogContentContainer(info=lbl_text, image=image, size_hint=(1, 1)
),
MDDialogButtonContainer(
Widget(),
MDButton(
MDButtonText(text="Отмена"),
on_press=self.on_cancel_button_press),
spacing="8dp"),
size_hint=(1, 1))
self.dialog.open()
def on_enter(self, *args):
main_widget = PreviousExercises()
self.add_widget(main_widget)
main_widget.set_list_md_icons()
def on_cancel_button_press(self, instance):
self.dialog.dismiss()
def set_previous_screen(self, screen):
self.previous_screen = screen
class CustomMDDialogContentContainer(MDDialogContentContainer):
def __init__(self, *args, info, image, **kwargs):
super().__init__(*args, **kwargs)
self.orientation = "vertical"
layout_root = MDScrollView(do_scroll_x=False, do_scroll_y=True, size_hint_y=None)
layout = MDBoxLayout(orientation="vertical", size_hint=(1, None), adaptive_height=True,
padding="10dp", spacing="10dp")
layout.add_widget(MDDivider())
img = FitImage(source=path.join("images", image.replace("\n", "")), size_hint=(1, None),
keep_ratio=True, allow_stretch=True, height="300dp")
layout.add_widget(img)
layout.add_widget(MDDivider())
for text_item in info:
layout.add_widget(MDLabel(text=text_item, size_hint_y=None, adaptive_height=True))
layout.add_widget(MDDivider())
layout_root.add_widget(layout)
self.add_widget(layout_root)
class CustomOneLineIconListItem(MDListItem):
icon = StringProperty()
text = StringProperty()
class PreviousExercises(MDScreen):
def set_list_md_icons(self, text="", search=False):
"""Builds a list of exercises for the FourthScreen."""
def add_exercise_item(name_exercise):
self.ids.rv.data.append(
{
"viewclass": "CustomOneLineIconListItem",
"text": name_exercise,
"callback": lambda x: x,
}
)
self.ids.rv.data = []
for name_exercise in exercise_items:
if search:
if text in name_exercise:
add_exercise_item(name_exercise)
else:
add_exercise_item(name_exercise)
class MainApp(MDApp):
def build(self):
self.sm = MDScreenManager()
self.sm.add_widget(FourthScreen(name="Fourth"))
return self.sm
if __name__ == "__main__":
app = MainApp()
app.run()
您可以尝试将
MDScrollView
的高度设置为App
的高度。在您的 CustomMDDialogContentContainer.__init__()
更改中:
layout_root = MDScrollView(do_scroll_x=False, do_scroll_y=True, size_hint_y=None)
至:
app = MDApp.get_running_app()
layout_root = MDScrollView(do_scroll_x=False, do_scroll_y=True, size_hint_y=None, height=app.root.height)