Button(按钮)

大约 2 分钟

Button(按钮)

按钮

组件使用示例open in new window

属性

除了支持所有基础属性,还支持以下属性:

titleAttr方法

设置文本属性

参数描述类型
init文本属性TextAttr

imageAttr方法

设置图片属性

参数描述类型
init图片属性ImageAttr

highlightBackgroundColor方法

设置按下态颜色

参数描述类型
color颜色Color

flexDirection方法

设置图标和文本的排列方向

参数描述类型
flexDirection排列方向enum

glassEffectIOS

系统要求

iOS 26.0+ 支持。在低版本系统上会自动降级到普通背景效果。

为Button组件添加iOS 26+液态玻璃效果。

参数描述类型默认值
enable是否启用液态玻璃效果Booleantrue
interactive是否启用交互效果Booleantrue
tintColor玻璃效果色调颜色Color系统默认
style玻璃效果样式(REGULAR/CLEAR)GlassEffectStyleREGULAR

GlassEffectStyle 枚举值:

  • REGULAR - 标准液态玻璃效果
  • CLEAR - 清透液态玻璃效果

示例:

Button {
    attr {
        glassEffectIOS() // 默认效果
        // 或自定义效果
        glassEffectIOS(
            style = GlassEffectStyle.CLEAR,
            tintColor = Color.BLUE,
            interactive = true
        )
    }
    titleAttr {
        text("液态玻璃按钮")
    }
}

注意

使用glassEffectIOS时,不能同时设置backgroundColor,否则会覆盖玻璃效果。

glassEffectContainerIOS

系统要求

iOS 26.0+ 支持。在低版本系统上会自动降级到普通容器效果。

为Button组件添加液态玻璃容器效果,用于创建多个液态玻璃元素之间的融合效果。

参数描述类型默认值
spacing子元素之间的间距Float0f

示例:

View {
    attr {
        glassEffectContainerIOS(10f) // 10点间距
        flexDirectionRow()
    }
    
    Button {
        attr {
            flex(1f)
            glassEffectIOS()
            backgroundColor(Color.TRANSPARENT)
        }
        titleAttr {
            text("左按钮")
        }
    }
    
    Button {
        attr {
            flex(1f)
            glassEffectIOS()
            backgroundColor(Color.TRANSPARENT)
        }
        titleAttr {
            text("右按钮")
        }
    }
}

兼容性处理

Button {
    attr {
        if (PlatformUtils.isLiquidGlassSupported()) {
            glassEffectIOS()
            backgroundColor(Color.TRANSPARENT)
        } else {
            backgroundColor(Color(0x80000000)) // 半透明黑色降级
        }
    }
    titleAttr {
        text("自适应按钮")
    }
}

事件

支持所有基础属性