如何在不使用kv语言的情况下使用KivyMD创建导航栏?

时间:2019-11-27 12:48:30

标签: python kivy navigation-drawer

我想在我的android应用程序中创建一个导航栏。我在google中搜索了很多内容。我得到了解决方案,但是它是基于kv语言的。我需要在不使用kv语言的情况下进行创建。

我不喜欢奇异的语言。我只喜欢python

欢迎任何意见

非常感谢

1 个答案:

答案 0 :(得分:0)

带有 KivyMD 的导航抽屉的应用(带有kv代码):

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.navigationdrawer import NavigationLayout  # Do not remove

root_kv = """
<ContentNavigationDrawer@MDNavigationDrawer>:
    drawer_logo: "drawer_logo.png"

    NavigationDrawerSubheader:
        text: "Menu:"

    NavigationDrawerIconButton:
        icon: "access-point"
        text: "First"

    NavigationDrawerIconButton:
        icon: "account"
        text: "Second"


NavigationLayout:
    id: nav_layout

    ContentNavigationDrawer:
        id: nav_drawer

    BoxLayout:
        orientation: "vertical"

        MDToolbar:
            id: toolbar
            title: "Toolbar title"
            md_bg_color: app.theme_cls.primary_color
            background_palette: "Primary"
            background_hue: "500"
            elevation: 10
            left_action_items:
                [["menu", lambda x: app.root.toggle_nav_drawer()]]

        BoxLayout:
            orientation: "vertical"
            padding: dp(16)

            MDRaisedButton:
                text: "Click me!"

            Widget:
"""


class MainApp(MDApp):

    def build(self):
        self.root = Builder.load_string(root_kv)


if __name__ == "__main__":
    MainApp().run()

没有kv代码的同一应用程序(也没有与应用程序主题颜色的绑定,没有ID,也没有其他有用的东西)。 (仅适用于master分支中的KivyMD,请参见how to install

from kivy.metrics import dp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.navigationdrawer import (
    NavigationLayout,
    MDNavigationDrawer,
    NavigationDrawerSubheader,
    NavigationDrawerIconButton,
)
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.button import MDRaisedButton


class MainApp(MDApp):
    def build(self):
        nav_layout = NavigationLayout()
        nav_drawer = MDNavigationDrawer(drawer_logo="drawer_logo.png")
        nav_drawer.add_widget(NavigationDrawerSubheader(text="Menu:"))
        nav_drawer.add_widget(
            NavigationDrawerIconButton(icon="access-point", text="First")
        )
        nav_drawer.add_widget(NavigationDrawerIconButton(icon="account", text="Second"))
        box_layout = BoxLayout(orientation="vertical")
        toolbar = MDToolbar(
            title="Toolbar title",
            md_bg_color=self.theme_cls.primary_color,
            background_palette="Primary",
            background_hue="500",
            elevation=10,
        )
        toolbar.left_action_items = [["menu", lambda x: self.root.toggle_nav_drawer()]]
        box_layout.add_widget(toolbar)
        box_layout_content = BoxLayout(orientation="vertical", padding=dp(16))
        box_layout_content.add_widget(MDRaisedButton(text="Click me!"))
        box_layout_content.add_widget(Widget())
        box_layout.add_widget(box_layout_content)
        nav_layout.add_widget(nav_drawer)
        nav_layout.add_widget(box_layout)
        self.root = nav_layout


if __name__ == "__main__":
    MainApp().run()

我认为使用kv语言的应用程序代码更具可读性,而且看起来不错。如果您不使用kv语言编写所有应用程序代码,那么它将看起来很糟糕,难以编辑,并且以后还会出现许多错误。因此,我建议您学习kv语言并用它编写程序。