kotlin 如何在Jetpack合成中设置ExtendedFloatingActionButton的默认高度

c86crjj0  于 2022-12-19  发布在  Kotlin
关注(0)|答案(2)|浏览(202)

如何在Jetpack Compose中应用ExtendedFloatingActionButton的默认标高,以便可以看到阴影?可以理解,默认静止标高是3号,默认高度是6dp,但使用elevation = 6.dp不起作用,那么还有什么其他方法呢?

ExtendedFloatingActionButton(
    text = { Text(text = "Button") },
    icon = { Icon(Icons.Filled.Add, "") },
    modifier = Modifier.clip(RoundedCornerShape(16.dp)),
    elevation = FloatingActionButtonElevation.?
)
insrf1ej

insrf1ej1#

对于M3,默认标高由FloatingActionButtonDefaults.elevation()定义。
如果需要默认值,只需忽略它。您还可以使用以下命令覆盖默认值:

ExtendedFloatingActionButton(
    //..
    elevation = FloatingActionButtonDefaults.elevation(
        defaultElevation = 4.dp
    )
)

它定义不同状态下ExtendedFloatingActionButton的高程

@Composable
fun elevation(
    defaultElevation: Dp = FabPrimaryTokens.ContainerElevation, //6.0.dp
    pressedElevation: Dp = FabPrimaryTokens.PressedContainerElevation, //6.0.dp
    focusedElevation: Dp = FabPrimaryTokens.FocusContainerElevation, //6.0.dp
    hoveredElevation: Dp = FabPrimaryTokens.HoverContainerElevation //8.0.dp
): FloatingActionButtonElevation

您可以在source code中看到:

val ContainerElevation = ElevationTokens.Level3
val PressedContainerElevation = ElevationTokens.Level3
val FocusContainerElevation = ElevationTokens.Level3
val HoverContainerElevation = ElevationTokens.Level4

对于M2,方法类似,默认值由FloatingActionButtonDefaults.elevation()定义:

@Composable
fun elevation(
    defaultElevation: Dp = 6.dp,
    pressedElevation: Dp = 12.dp,
    hoveredElevation: Dp = 8.dp,
    focusedElevation: Dp = 8.dp,
)
szqfcxe2

szqfcxe22#

默认值可能是颜色、标高或任何地方,应该像这样使用

ExtendedFloatingActionButton(
    text = { Text(text = "Button") },
    icon = { Icon(Icons.Filled.Add, "") },
    modifier = Modifier.clip(RoundedCornerShape(16.dp)),
    elevation = FloatingActionButtonDefaults.elevation(
        defaultElevation = 4.dp,
        pressedElevation = 8.dp,
        hoveredElevation = 2.dp,
        focusedElevation = 1.dp,
    ),
    onClick = {}
)

相关问题