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
相关产品推荐
相关产品推荐

