【问题标题】:How do i make an MDBottomAppBar appear on top of a list view in KivyMD如何使 MDBottomAppBar 出现在 KivyMD 的列表视图顶部
【发布时间】:2021-03-22 00:18:38
【问题描述】:

如何让 MDBottomAppBar 出现在 KivyMD 的列表视图顶部

from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.list import MDList, TwoLineAvatarListItem, OneLineIconListItem, ImageLeftWidget
from kivy.graphics import Canvas, Ellipse
from kivymd.uix.button import MDRaisedButton, MDFlatButton, MDRectangleFlatButton

KV = '''
Screen:
    BoxLayout:
        orientation : 'vertical'
        MDToolbar:
            id: toolbar
            md_bg_color: (41 / 255, 201 / 255, 108 / 255, 1)
            title: "WhatsApp"
            left_action_items: [['menu', lambda x: navdraw.set_state("open")]]
            right_action_items: [['magnify', lambda x: None], ['dots-vertical', lambda x: None]]
        Widget:
        
        MDBottomAppBar:
            md_bg_color: (41 / 255, 201 / 255, 108 / 255, 1)
            id : bottombar
            
            MDToolbar:
                icon: 'share-variant'
                type: 'bottom'
                left_action_items: [['home', lambda x: root.manager.current("HomeScreen")]]
                right_action_items: [['circle', lambda x: None], ['dots-vertical', lambda x: None]]
                mode : "center"
    
    MDNavigationLayout:
        size_hint_y : 1.0 - toolbar.height/root.height
        ScreenManager :
            id : screen_manager
            Screen:
                name : 'HomeScreen'
                BoxLayout:
                    orientation: "vertical"
                    MDTabs:
                        tab_hint_x: True
                        background_color: (41 / 255, 201 / 255, 108 / 255, 1)
                        text_color: (255 / 255, 255 / 255, 255 / 255, 1)
                        halign: 'center'

                        Tab:
                            title: 'CHATS'
                            ScrollView:
                                MDList:
                                    id: tabone

                        Tab:
                            title: 'CALLS'
                            ScrollView:
                                index : 1
                                MDList:
                                    OneLineAvatarListItem:
                                        text: 'Eric Douglas'
                                        ImageLeftWidget:
                                            canvas:
                                                Ellipse:
                                                    source: 'adana.png'
                                                    size: 10, 10
                                                    angle_start: 0
                                                    angle_end: 360

                        Tab:
                            text: 'CALLS'
            ScreenOne:
            
        MDNavigationDrawer:
            id : navdraw
            BoxLayout:
                orientation : 'vertical'
                MDRectangleFlatButton:
                    text: 'Click Me'
                    on_press : 
                        navdraw.set_state("close")
                        screen_manager.current = "firstscreen"
                ScrollView:
                    MDList:
                        id:navdrawer
        
<ScreenOne>:
    name : "firstscreen"
    BoxLayout:
        orientation : 'vertical'
        MDRectangleFlatButton:
            text : "Go to first Screen"
            on_press : root.manager.current = "HomeScreen"
        ScrollView:
            MDList :
                OneLineIconListItem:
                    text: 'FEDERAL GOVERNMENT KILL 300 BOKO HARAM'
                    IconLeftWidget : 
                        icon: 'clock'
'''

    class ScreenOne(Screen):
    pass


sm = ScreenManager()
sm.add_widget(ScreenOne(name="firstscreen"))


class Tab(FloatLayout, MDTabsBase):
    '''Class implementing content for a tab.'''


class Example(MDApp):
    def build(self):
        self.theme_cls.accent_palette = 'Green'
        return Builder.load_string(KV)

    def on_start(self):
        self.my_list()
        self.chat_list()

    def my_list(self):
        for i in range(20):
            list = TwoLineAvatarListItem(text="Hello world")
            list.add_widget(ImageLeftWidget(source='adana.png'))
            self.root.ids.navdrawer.add_widget(list)

    def chat_list(self):
        for i in range(20):
            list = TwoLineAvatarListItem(text="Hello world", on_release=self.printout)
            list.add_widget(ImageLeftWidget(source='adana.png'))
            self.root.ids.tabone.add_widget(list)

    def printout(self, hello):
        hello = "Hello world"
        print(hello)


Example().run()

example of problem encountered

请问我如何让页面的内容放在 MDBottomAppBar 后面,我需要用标签绑定一些参数,这样我可以在点击时获取参数,就像使用 get 和 post 获取参数一样与网页设计中的锚元素绑定

【问题讨论】:

    标签: python mobile kivy kivy-language kivymd


    【解决方案1】:

    例如:

    from kivy.lang import Builder
    from kivymd.app import MDApp
    
    KV = '''
    <CustomOneLineListItem@OneLineListItem>
        text: "Single-line item"
        
    Screen:
        MDBoxLayout
            orientation: 'vertical'
            padding: 0,0,0,bar.height
            ScrollView:
                bar_width: 4
                MDList:
                    CustomOneLineListItem:
                    CustomOneLineListItem:
                    CustomOneLineListItem:
                    CustomOneLineListItem:
                    CustomOneLineListItem:
                    
        MDBottomAppBar:
            MDToolbar:
                id: bar
                title: "Title"
                icon: "git"
                type: "bottom"
                left_action_items: [["menu", lambda x: x]]
    '''
    
    
    class Test(MDApp):
        def build(self):
            return Builder.load_string(KV)
    
    
    Test().run()
    

    请不要附上这么多的代码,很少有人会想读它,在你的情况下根本没有必要,我建议你附上不超过100行代码

    【讨论】:

    • 谢谢,它成功了。我使用bottombar.height
    • 如果它有助于将问题标记为解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多