gpt4 book ai didi

python - KivyMD : Contents of Screen overlap with Toolbar

转载 作者:行者123 更新时间:2023-12-05 03:54:06 26 4
gpt4 key购买 nike

我正在 KivyMD 中编写一个简单的应用程序。根据 kivy 文档,我的 .kv 结构与 MDNavigationDrawer 和 MDToolbar 是正确的,只要屏幕是空的,一切都可以正常工作。当我向它们添加内容时,内容不是在工具栏下面而是在它上面。我该如何解决?

这是我的代码:

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty

from kivymd.app import MDApp

KV = '''
<ContentNavigationDrawer>:

ScrollView:

MDList:

OneLineListItem:
text: "Screen 1"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 1"

OneLineListItem:
text: "Screen 2"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"


Screen:

MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
title: "Test"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

NavigationLayout:
x: toolbar.height

ScreenManager:
Screen:
name: "scr 1"
ScrollView:
BoxLayout:
orientation: "vertical"
Button:
text: "Hello"
Button:
text: "I wish"
Button:
text: "I could"
Button:
text: "Finally get"
Button:
text: "This to work"


Screen:
name: "scr 2"

MDLabel:
text: "Screen 2"
halign: "center"

MDNavigationDrawer:
id: nav_drawer

ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
'''


class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()


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


TestNavigationDrawer().run()

以下代码的输出: Output of the code

最佳答案

Screen 类是一个 RelativeLayout,因此您必须像定位任何 RelativeLayout 一样定位它的子级。 Screen 的每个 child 都获得 (1,1) 的默认 size_hint 的默认 pos >(0,0),所以如果不是你想要的,你必须调整它。在您的情况下,NavigationLayout 将根据这些默认值完全覆盖 Screen。您可以通过添加 size_hint_y 来解决这个问题,如下所示:

NavigationLayout:
x: toolbar.height
size_hint_y: 1.0 - toolbar.height/root.height

这会设置 NavigationLayout 的大小,使其刚好与 MDToolbar 的底部相交,而不是覆盖它。

关于python - KivyMD : Contents of Screen overlap with Toolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61318726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com