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

KivyMD ScrollView always_scroll失效求助:滚动后自动回弹

解决Kivy ScrollView无法正常滚动的问题

问题根源

你的ScrollView滚动异常,核心原因是使用MDFloatLayout作为子容器,且通过pos_hint: {"center_y": ...}手动定位所有元素。这种方式下,容器的实际高度并未被计算为所有子元素的总高度,ScrollView会判定内容未超出视口范围,因此限制滚动并触发回弹。

修复方案

  1. 将内部的MDFloatLayout替换为MDBoxLayout(垂直布局场景下,BoxLayout会自动按顺序排列子元素,无需手动定位)
  2. 删除所有控件中的pos_hint: {"center_y": ...}配置,让BoxLayout自动管理元素位置
  3. 给MDBoxLayout添加spacing属性,通过这个参数控制元素间的间距,替代手动定位产生的空隙
  4. 保持MDBoxLayout的adaptive_height: True,确保容器高度自适应所有子元素的总高度

修改后的完整代码

ScrollView:
    id: scroll_view
    # always_scroll: True  # 此属性非必须,内容超出视口时默认允许滚动
    MDBoxLayout:
        adaptive_height: True
        orientation:'vertical'
        spacing: 20  # 控制元素间的垂直间距,可根据需求调整
        padding: [10, 20]  # 给容器添加内边距,优化视觉效果

        MDLabel:
            text: "DISEASE NAME"
            font_name: "Raleway-Black.ttf"
            font_size: 18
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[10,0]
            adaptive_height: True

        MDLabel:
            text: "Vascular Streak Dieback"
            font_name: "Raleway-Black.ttf"
            font_size: 17
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[20,0]

        MDBoxLayout:
            size_hint: .95, None
            height: '1dp'
            pos_hint: {"center_x":.5}
            padding: "0dp"
            spacing: "0dp"
            orientation: "vertical"
            elevation: 0
            md_bg_color:(0.627, 0.510, 0.392, 1.0)
        
        MDLabel:
            text: "CAUSAL TYPE"
            font_name: "Raleway-Black.ttf"
            font_size: 18
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[10,3]

        MDLabel:
            text: "Fungi"
            font_name: "Raleway-Black.ttf"
            font_size: 16
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[20,0]
            adaptive_height: True

        MDBoxLayout:
            size_hint: .95, None
            height: '1dp'
            pos_hint: {"center_x":.5}
            padding: "0dp"
            spacing: "0dp"
            orientation: "vertical"
            elevation: 0
            md_bg_color:(0.627, 0.510, 0.392, 1.0)

        MDLabel:
            text: "CROP STAGE AFFECTED"
            font_name: "Raleway-Black.ttf"
            font_size: 18
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[10,0]
            adaptive_height: True

        MDLabel:
            text: "Pre-emergence, Seeding stage, Vegetative growing stage, Flowering stage, Fruiting stage"
            font_name: "Raleway-Black.ttf"
            font_size: 16
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[20,0]
        
        MDBoxLayout:
            size_hint: .95, None
            height: '1dp'
            pos_hint: {"center_x":.5}
            spacing: "0dp"
            orientation: "vertical"
            elevation: 0
            md_bg_color:(0.627, 0.510, 0.392, 1.0)
    
        MDLabel:
            text: "SYMPTOMS BY PART"
            font_name: "Raleway-Black.ttf"
            font_size: 18
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[10,0]
            adaptive_height: True

        MDLabel:
            text: "Leaves - discoloration and abnormal with necrotic area ,Stems - mold growth, bark discolorations and dieback , Roots - lesions with necrotic streaks, Whole plant - dwarfing"
            font_name: "Raleway-Black.ttf"
            font_size: 16
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[20,0]
            adaptive_height: True

        MDBoxLayout:
            size_hint: .95, None
            height: '1dp'
            pos_hint: {"center_x":.5}
            spacing: "0dp"
            orientation: "vertical"
            elevation: 0
            md_bg_color:(0.627, 0.510, 0.392, 1.0)

        MDLabel:
            text: "SIGNS AND SYMPTOMS"
            font_name: "Raleway-Black.ttf"
            font_size: 18
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[10,0]
            adaptive_height: True
        
        MDLabel:
            text: "Symptoms may vary from time to time, initially, chlorosis of the leaf may observed. Chlorosis may begin either at the tip or base of the leaf. Falling of leaves may observed in mid range."
            font_name: "Raleway-Black.ttf"
            font_size: 16
            halign: "left"
            theme_text_color: "Custom"
            text_color: "#A08264"
            padding:[20,0]
            adaptive_height: True

额外说明

  • always_scroll: True并非必要属性,只要子容器的总高度超过ScrollView的视口高度,ScrollView默认就会允许滚动
  • 避免用pos_hint的负数值来定位元素,这种方式会破坏布局的自动计算逻辑,导致ScrollView无法正确识别内容高度
  • 原代码中有一个重复的"CAUSAL TYPE"标题,我已将其修正为"SYMPTOMS BY PART",如果不需要可以自行调整

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 02:32:48