android 按钮点击时没有涟漪效应(Jetpack组合)

np8igboo  于 2023-01-19  发布在  Android
关注(0)|答案(3)|浏览(172)

当然,Jetpack compose创建的按钮在默认情况下应该在点击时有涟漪效应。但是我的按钮在点击时没有显示涟漪效应。下面是我的按钮的代码:

BoxWithConstraints(modifier = Modifier
                .constrainAs(button) {
                    top.linkTo(glTopButtonProceed)
                    start.linkTo(glLeftBtn)
                    end.linkTo(glRightBtn)
                    width = Dimension.fillToConstraints

                }) {

                Button(
                    onClick = {
                        navController.navigate("autosave_screen")
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(if (screenHeight>=700.dp)
                            50.dp
                        else
                            with(LocalDensity.current) {dimensionResource(id = R.dimen._35sdp)}) ,


                    colors = if (text.text != "" && text.text.length == 4)
                        ButtonDefaults.buttonColors(backgroundColor = colorResource(id = R.color.bright_green))
                    else
                        ButtonDefaults.buttonColors(backgroundColor = colorResource(id = R.color.gray))

                ) {
                    Text(
                        "Proceed", color = colorResource(id = R.color.dark_blue),
                        fontSize =
                        if (screenHeight>=700.dp)
                            19.sp
                        else
                            with(LocalDensity.current) {dimensionResource(id = R.dimen._12sdp).toSp()},
                        fontFamily = FontFamily(Font(R.font.poppins_medium)),

                        textAlign = TextAlign.Center,
                        modifier = Modifier.align(Alignment.CenterVertically)
                    )

                }

            }
        }

如何为我的按钮启用涟漪效应?

nvbavucw

nvbavucw1#

您可以尝试在修饰符click上使用click,而不是在Button上调用onClick

modifier = Modifier .clickable(
 interactionSource = remember { MutableInteractionSource() },
    indication = rememberRipple(bounded = false),
    onClick = {}
        )

您也可以自定义按钮涟漪。

vuv7lop3

vuv7lop32#

用材质主题 Package 你的盒子,获得不错的涟漪效果

tyg4sfes

tyg4sfes3#

如果你想给你的按钮赋予特定的颜色,试着在Text Composable上使用Modifier.fillMaxSize().background(...)。不要使用Buttoncolors参数。

相关问题