Android Jetpack Compose - 自定义图像的浮动操作按钮

6

我编写了这段代码。

    Image (
        painter = painterResource(id = R.drawable.image),
        contentDescription = "fab",
        modifier = Modifier
            .size(80.dp)
            .clickable(
                interactionSource = remember { MutableInteractionSource() },
                indication = rememberRipple(true),
                onClick = { /* TODO */ }
            )
    )

但是,我使用Scaffold。 因此,我想使用浮动操作按钮或扩展浮动操作按钮。 我该如何使用浮动操作按钮和自定义图像。 我有一张圆形照片要用于按钮。

    Scaffold(
        topBar = {},
        content = {},
        floatingActionButton = {
            Image {
                /* How can I wrire */
            }
        }
    )
2个回答

9
您可以使用类似以下的代码:
Scaffold(
    topBar = {},
    content = {},
    floatingActionButton = {
        FloatingActionButton(onClick = { /*TODO*/ }) {
            Image (
                painter = painterResource(id = R.drawable.xxxx),
                contentDescription = "fab",
                contentScale = ContentScale.FillBounds
            )
        }
    }
)

enter image description here


非常感谢!但是,这个图像周围有一个边距。 - vr_min_chan
@vr_min_chan 添加适当的contentScale(请检查更新后的答案)。 - Gabriele Mariotti

0
 FloatingActionButton(
                        backgroundColor = Color(0xFFF7FAFF),
                        onClick = { **TODO** },
                        modifier = Modifier.align(Alignment.BottomEnd).padding(start = 15.dp, top = 15.dp, end = 8.dp, bottom = 15.dp).background(Color.Transparent)) 
                        {
                        Image(painter = painterResource(id = R.drawable.icon_fab_btn), contentDescription = "fab", contentScale = ContentScale.FillBounds,)
                        }

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接