前言
除了布局组件外,Jetpack Compose 还提供了一系列其他常用的 UI 组件。
组件
https://developer.android.google.cn/jetpack/compose/components?hl=zh-cn
入门教程
https://developer.android.google.cn/courses/pathways/compose?hl=zh-cn
按钮
基本使用
1 | Button( |
自定义按钮
效果
组件定义
1 | import androidx.compose.foundation.background |
使用
1 | Box( |
背景色
- 推荐使用
ButtonDefaults.buttonColors
:它会自动处理按钮的各种状态(按压、禁用等),且符合 Material Design 设计规范。 containerColor
参数:在 Material3 中,containerColor
用于设置按钮的背景色(替代了 Material2 中的backgroundColor
)。- 自定义背景时:需将
containerColor
设为Color.Transparent
以清除默认背景,避免样式冲突。 - 形状一致性:自定义背景时建议使用
ButtonDefaults.shape
保持默认圆角,或通过shape
参数指定自定义形状。
示例
1 | Button( |
注意
通过background设置是不生效的。
单选按钮
1 |
|
Switch
1 |
|