简体   繁体   中英

Align title on center of MDToolbar

How to align the title on center of MDToolbar?

Bellow is the code:

from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp

class MyLayout(BoxLayout):

    scr_mngr = ObjectProperty(None)

    def change_screen(self, screen, *args):
        self.scr_mngr.current = screen


KV = """
MyLayout:
    scr_mngr: scr_mngr
    orientation: 'vertical'
    MDToolbar:
        id: toolbar
        title: 'My App'
        anchor_title: 'center'
        right_action_items: [['settings', lambda x: root.change_screen('profile') ]]
    ScreenManager:
        id: scr_mngr
        Screen:
            name: 'profile'
"""


class MyApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def build(self):
        return Builder.load_string(KV)


MyApp().run()

The toolbar now look like this, but i want to be the text in the green box

toolbar image

I had the same question.

All credit to Xyanight for his answer on this link that helped me find the answer: KivyMD How to change MDToolbar title size and font?

Go to this link and add the following accordingly:

Clock.schedule_once(self.set_toolbar_title_halign)
        
    def set_toolbar_title_halign(self, *args):
        self.ids.toolbar.ids.label_title.halign = "center"

Here is Xyanight's exmple with a little tweak for the title alignment:

from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

from kivymd.app import MDApp

kv = Builder.load_string(
    """
<SM>
    P1:


<P1>

    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            id: toolbar
            title: 'TEST'
""")


class P1(Screen):
    def __init__(self, **kw):
        super().__init__(**kw)
        Clock.schedule_once(self.set_toolbar_title_halign)
        Clock.schedule_once(self.set_toolbar_font_size)

    def set_toolbar_title_halign(self, *args):
        self.ids.toolbar.ids.label_title.halign = "center"

    def set_toolbar_font_size(self, *args):
        self.ids.toolbar.ids.label_title.font_size = '50sp'


class SM(ScreenManager):
    pass


class MyApp(MDApp):
    def build(self):
        return SM()


if __name__ == '__main__':
    MyApp().run()

This is my first time contributing.

You can simply use anchor_title: "center" in your kv string.

your code should be like this:

kv = Builder.load_string(
    """
<SM>
    P1:


<P1>

    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            id: toolbar
            title: 'TEST'
            anchor_title: "center"
""")

halign: "center",anchor_title: "center" both are not woking for me and I'm usinng kivymd 1.0.1 version

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM