我正在用KivyMD编写一个简单的应用程序。根据kivy文档,我的带有MDNavigationDrawer和MDToolbar的.kv结构是正确的,只要屏幕为空,一切正常。当我向他们添加内容时,该内容而不是位于工具栏下方。我该如何解决?
这是我的代码:
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()
答案 0 :(得分:1)
Screen
类是RelativeLayout
,因此您必须像放置任何RelativeLayout
一样放置其子级。 Screen
的每个子级都获得size_hint
的默认(1,1)
和pos
的默认(0,0)
,因此如果这不是您想要的,则必须进行调整。对于您而言,NavigationLayout
将基于这些默认值完全覆盖Screen
。您可以通过添加size_hint_y
来解决此问题,如下所示:
NavigationLayout:
x: toolbar.height
size_hint_y: 1.0 - toolbar.height/root.height
这将设置NavigationLayout
的大小,以使其恰好位于MDToolbar
的底部,而不是重叠。