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

KivyMD ScrollView添加多组TextField与Button时滚动及布局异常问题

核心修改点

  • 给ScrollView内的GridLayout添加height: self.minimum_height绑定,让GridLayout高度自适应所有子组件的总高度,只有子组件总高度大于ScrollView可视区域高度时才能触发滚动
  • 将所有组件的无效属性pos_hint_x:0.5替换为合法属性pos_hint: {"center_x": 0.5},实现组件水平居中
  • 修正MDTextField多行属性的拼写错误,补充按钮的尺寸和对齐配置,优化边距显示效果

修改后完整代码

from kivymd.app import MDApp
from kivy.lang import Builder

KV = """ 
Screen:
    MDToolbar:
        title:'Mini Agriculturist'
        pos_hint:{'top':1}
        left_action_items : [["arrow-left"]]
        right_action_items : [["dots-vertical"]]
        elevation:15

    ScrollView:
        pos_hint:{'top':0.88}
        size_hint_y: 0.88
        GridLayout:
            cols: 1
            size_hint_y: None
            height: self.minimum_height
            spacing:'10dp'
            padding: [0, '20dp', 0, '20dp']
            MDTextField:
                hint_text: "Category"
                mode: "rectangle"
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
                multiline: True
                id: Test_ss
            MDTextField:
                hint_text: "Disease name"
                mode: "rectangle"  
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
            MDTextField:
                hint_text: "Symptoms"
                mode: "rectangle"
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
                multiline: True  
            MDTextField:
                hint_text: "More about disease"
                mode: "rectangle" 
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
                multiline: True  
            MDTextField:
                hint_text: "Category"
                mode: "rectangle" 
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
                multiline: True
            MDTextField:
                hint_text: "Disease name"
                mode: "rectangle"  
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
                multiline: True
            MDTextField:
                hint_text: "Symptoms"
                mode: "rectangle"
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
                multiline: True 
            MDTextField:
                hint_text: "More about disease"
                mode: "rectangle" 
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
            MDTextField:
                hint_text: "Disease name"
                mode: "rectangle"  
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}
            MDTextField:
                hint_text: "Symptoms"
                mode: "rectangle"
                size_hint_x:0.4
                pos_hint: {"center_x": 0.5}   
            MDRectangleFlatButton:
                text:'Choose'
                font_size:14
                pos_hint: {"center_x": 0.5}
                size_hint_x: 0.4
            MDRectangleFlatButton:
                text:'Cancel'
                font_size:14
                pos_hint: {"center_x": 0.5}
                size_hint_x: 0.4

"""

class WeatherApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def on_start(self):
        self.root.ids.Test_ss.text = " Dhur bal\nOi salar putera\nMod khaba naki "
    def build(self):
        self.screen = Builder.load_string(KV)
        return self.screen
WeatherApp().run()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 01:30:04