Compose 的 Button
是基于
Material Design 理念设计的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Composable fun Button( onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, elevation: ButtonElevation? = ButtonDefaults.elevation(), shape: Shape = MaterialTheme.shapes.small, border: BorderStroke? = null, colors: ButtonColors = ButtonDefaults.buttonColors(), contentPadding: PaddingValues = ButtonDefaults.ContentPadding, content: @Composable RowScope.() -> Unit ): Unit
|
基本使用
1 2 3 4 5 6 7 8 9 10 11
| Button(onClick = { }) { Icon( Icons.Filled.Favorite, contentDescription = null, modifier = Modifier.size(ButtonDefaults.IconSize) ) Spacer(Modifier.size(ButtonDefaults.IconSpacing)) Text("喜欢") }
|
不同点击状态下的按钮状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| data class ButtonState(var text: String, var textColor: Color, var buttonColor: Color)
val interactionState = remember { MutableInteractionSource() }
val (text, textColor, buttonColor) = when { interactionState.collectIsPressedAsState().value -> ButtonState("Just Pressed", Color.Red, Color.Black) else -> ButtonState( "Just Button", Color.White, Color.Red) }
Button( onClick = { }, interactionSource = interactionState, elevation = null, shape = RoundedCornerShape(50), colors = ButtonDefaults.buttonColors( backgroundColor = buttonColor, ), modifier = Modifier.width(IntrinsicSize.Min).height(IntrinsicSize.Min) ) { Text( text = text, color = textColor ) }
|
OutlinedButton
是有轮廓线的按钮。
1 2 3 4 5
| @Composable @NonRestartableComposable fun OutlinedButton( ): Unit
|
文本按钮(TextButton)
文本按钮通常用于不太明显的操作,包括那些位于对话框和卡片中的操作。内部Text
组件的默认文本样式将设置为Typography.button
。
1 2 3 4 5
| @Composable @NonRestartableComposable fun TextButton( ): Unit
|
IconButton 是一个可点击的图标,用于表示动作。IconButton
的整体最小触摸目标尺寸为 48 x
48dp,以满足无障碍指南。content
在 IconButton 内居中。
1 2 3 4 5 6 7 8 9 10 11 12
| @Composable fun IconButton( onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, content: @Composable () -> Unit ): Unit
|
1 2 3
| IconButton(onClick = { }) { Icon(Icons.Filled.Favorite, contentDescription = "Localized description") }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Composable fun IconToggleButton( checked: Boolean, onCheckedChange: (Boolean) -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, content: @Composable () -> Unit ): Unit
|
1 2 3 4
| IconToggleButton(checked = checked, onCheckedChange = { checked = it }) { val tint by animateColorAsState(if (checked) Color(0xFFEC407A) else Color(0xFFB0BEC5)) Icon(Icons.Filled.Favorite, contentDescription = "Localized description", tint = tint) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Composable fun FloatingActionButton( onClick: () -> Unit, modifier: Modifier = Modifier, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)), backgroundColor: Color = MaterialTheme.colors.secondary, contentColor: Color = contentColorFor(backgroundColor), elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(), content: @Composable () -> Unit ): Unit
|
1 2 3
| FloatingActionButton(onClick = { }) { Icon(Icons.Filled.Favorite, contentDescription = "Localized description") }
|
带有图标 + 文字的浮动按钮。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Composable fun ExtendedFloatingActionButton( text: @Composable () -> Unit, onClick: () -> Unit, modifier: Modifier = Modifier, icon: (@Composable () -> Unit)? = null, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)), backgroundColor: Color = MaterialTheme.colors.secondary, contentColor: Color = contentColorFor(backgroundColor), elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation() ): Unit
|