简体   繁体   English

如何在 Jetpack Compose 中制作迷你工厂

[英]How to make a mini fab in Jetpack Compose

In XML we can make the floating action button in 2 sizes (regular and mini ) like the following在 XML 中,我们可以制作 2 种尺寸(常规和迷你)的浮动操作按钮,如下所示

  <com.google.android.material.floatingactionbutton.FloatingActionButton
  ...
  app:fabSize="mini"/>

My Question is how to make mini fab in Compose as the FloatingActionButton implementation as the follow (with no parameter for the size)我的问题是如何在Compose中制作迷你工厂作为FloatingActionButton实现如下(没有大小参数)

@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

As you can see in the FAB implementation it sets a default size.正如您在 FAB 实现中看到的那样,它设置了默认大小。 If you want to change the FAB size, I think the only way is using the Modifier.size(40.dp) .如果您想更改 FAB 大小,我认为唯一的方法是使用Modifier.size(40.dp)

FloatingActionButton(
    modifier = Modifier.size(40.dp), // 40 is a mini-fab
    onClick = {  }
) {
    Icon(imageVector = Icons.Filled.Add, contentDescription = null)
}

If you use androidx.compose.material3 in your project, you can use SmallFloatingActionButton如果你在项目中使用androidx.compose.material3 ,你可以使用SmallFloatingActionButton

@Composable
public fun SmallFloatingActionButton(
    onClick: () → Unit,
    modifier: Modifier,
    interactionSource: MutableInteractionSource,
    shape: Shape,
    containerColor: Color,
    contentColor: Color,
    elevation: FloatingActionButtonElevation,
    content: @Composable
() → Unit
): Unit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM