跳到主要内容

FloatingActionButton

@OptIn(markerClass = [ExperimentalMaterialApi])
@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: () -> Unit
): @OptIn(markerClass = [ExperimentalMaterialApi]) @Composable Unit

1. 概述

Material Design floating action button

一个 FloatActionButton(FAB)代表一个屏幕的主要行为。

FAB 通常和一个 Icon 一起使用

import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Icon

FloatingActionButton(onClick = { /*do something*/ }) {
Icon(Icons.Filled.Favorite, contentDescription = "Localized description")
}

除了普通的 FAB 之外,Compose 也提供了带有文字扩展的 FAB, ExtendedFloatingActionButton

ExtendedFloatingActionButton(
icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
text = { Text("添加到我喜欢的") },
onClick = { /*do something*/ }
)