You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KivyMD中AnchorLayout锚定位置不生效问题求助

问题:MDAnchorLayout锚定标签失效及文字错位问题

我想把“Owner's Information”标签锚定到黄色MDAnchorLayout的顶部中央,但设置锚点后并未生效。尝试将size_hint设为None后锚定生效,却出现标签文字字符错位,或需缩小尺寸才能让文字正常显示的问题。

实现代码:

from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.label import MDLabel
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.anchorlayout import MDAnchorLayout
from kivy.utils import get_color_from_hex
from kivy.core.text import LabelBase
from kivy.metrics import sp

class MainApp(MDApp):
    def build(self):
        screen = MDScreen(md_bg_color = get_color_from_hex("#CDE2FF"))

        layout_title = MDAnchorLayout(
            anchor_x='left', anchor_y='bottom',
            radius = [25, 25, 25, 25],
            md_bg_color = get_color_from_hex("#FFF000"),
            size_hint = (0.5, 0.5),
            pos_hint={"center_x": 0.325, "center_y": 0.7},
        )

        layout = MDFloatLayout(
            radius = [25, 25, 25, 25],
            md_bg_color = get_color_from_hex("#FFF9E6"),
            size_hint = (0.85, 0.6),
            pos_hint={"center_x": 0.5, "center_y": 0.4}
        )

        LabelBase.register(
            name="font50",
            fn_regular="font50.ttf"
        )

        self.theme_cls.font_styles["font50"] = {
            "large": {
                "line-height": 1.64,
                "font-name": "font50",
                "font-size": sp(50)
            }
        }

        label_title = MDLabel(
            text = "Owner's Information",
            font_style = "font50"
            )

        layout_title.add_widget(label_title)
        screen.add_widget(layout)
        screen.add_widget(layout_title)

        return screen

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

效果截图:效果截图


解决方案
  • 修正锚点方向:将layout_title的anchor_x改为center,anchor_y改为top,匹配你需要的顶部中央锚定需求。
  • 让标签自适应文字尺寸:MDLabel默认size_hint=(1,1)会占满父布局,导致锚定效果被掩盖。给标签设置size_hint=(None, None),再用size=self.texture_size让标签大小刚好包裹文字,同时设置halign='center'确保文字在标签内居中。
  • 简化字体设置:直接在MDLabel中指定字体参数,避免自定义font_style可能带来的冲突。

修改后的代码:

from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.label import MDLabel
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.anchorlayout import MDAnchorLayout
from kivy.utils import get_color_from_hex
from kivy.core.text import LabelBase
from kivy.metrics import sp

class MainApp(MDApp):
    def build(self):
        screen = MDScreen(md_bg_color = get_color_from_hex("#CDE2FF"))

        # 修正锚点为顶部中央
        layout_title = MDAnchorLayout(
            anchor_x='center', anchor_y='top',
            radius = [25, 25, 25, 25],
            md_bg_color = get_color_from_hex("#FFF000"),
            size_hint = (0.5, 0.5),
            pos_hint={"center_x": 0.325, "center_y": 0.7},
        )

        layout = MDFloatLayout(
            radius = [25, 25, 25, 25],
            md_bg_color = get_color_from_hex("#FFF9E6"),
            size_hint = (0.85, 0.6),
            pos_hint={"center_x": 0.5, "center_y": 0.4}
        )

        LabelBase.register(
            name="font50",
            fn_regular="font50.ttf"
        )

        # 调整标签参数,保证锚定生效且文字正常显示
        label_title = MDLabel(
            text="Owner's Information",
            font_name="font50",
            font_size=sp(50),
            size_hint=(None, None),
            size=self.texture_size,
            halign="center",
            valign="top"
        )

        layout_title.add_widget(label_title)
        screen.add_widget(layout)
        screen.add_widget(layout_title)

        return screen

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

关键说明:

  1. 锚点参数anchor_x='center'和anchor_y='top'直接定义了子控件在布局中的锚定位置,之前的left和bottom完全不符合需求。
  2. 设置size_hint=(None, None)+size=self.texture_size后,标签大小刚好匹配文字,锚定效果会直接作用在文字容器上,不再被占满布局的大盒子掩盖。
  3. 直接指定font_name和font_size比自定义theme字体样式更直观,减少不必要的样式冲突。

内容的提问来源于stack exchange,提问作者aki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 05:17:11