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

如何用Python和Kivy缩小或消除MDIconButton背景与内边距

解决MDIconButton背景与内边距过大问题

问题描述

我编写了一段创建MDIconButton的简单Python代码如下:

icon = MDIconButton(
    icon = "close-circle",
    md_bg_color = "red",
    theme_icon_color = "Custom",
    icon_color = "white",
    pos_hint = {"top": 1, "right": 1},
    icon_size = "18sp",
)

该按钮在目标Widget中尺寸过大,我尝试通过icon_size属性缩小它,但当图标尺寸调至一定值后,按钮背景会比图标本身更大。请问是否可以缩小甚至完全消除MDIconButton的背景及内边距?

解决方案

  • 完全消除背景:将md_bg_color设为transparent,直接去掉背景色:

    icon = MDIconButton(
        icon = "close-circle",
        md_bg_color = "transparent",
        theme_icon_color = "Custom",
        icon_color = "white",
        pos_hint = {"top": 1, "right": 1},
        icon_size = "18sp",
    )
    
  • 移除/缩小内边距:MDIconButton默认带有内边距,通过padding属性设置为0或自定义数值即可消除多余间距:

    icon = MDIconButton(
        icon = "close-circle",
        md_bg_color = "red",
        theme_icon_color = "Custom",
        icon_color = "white",
        pos_hint = {"top": 1, "right": 1},
        icon_size = "18sp",
        padding = 0,
    )
    
  • 同时移除背景和内边距:结合上述两个属性,让按钮仅显示图标:

    icon = MDIconButton(
        icon = "close-circle",
        md_bg_color = "transparent",
        theme_icon_color = "Custom",
        icon_color = "white",
        pos_hint = {"top": 1, "right": 1},
        icon_size = "18sp",
        padding = 0,
    )
    
  • 保留背景但匹配图标尺寸:如果需要保留背景,可通过radius属性调整背景大小,使其与图标尺寸一致(半径设为图标尺寸的一半):

    icon = MDIconButton(
        icon = "close-circle",
        md_bg_color = "red",
        theme_icon_color = "Custom",
        icon_color = "white",
        pos_hint = {"top": 1, "right": 1},
        icon_size = "18sp",
        padding = 0,
        radius = "9sp",
    )
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 20:30:18