compose Button颜色设置 边框设置

447 阅读1分钟

设置按钮颜色, colors = ButtonDefaults.buttonColors(

//背景色 backgroundColor = Color.White,

//边框 border = BorderStroke( width = 1.dp, color = colorResource(id = R.color.black_69) ),

//文字颜色 contentColor = Color.Black ),

image.png


Button(
    modifier = Modifier
        .width(100.dp)
        .padding(10.dp),
    border = BorderStroke(
        width = 1.dp,
        color = colorResource(id = R.color.black_69)
    ),
    colors = ButtonDefaults.buttonColors(
        backgroundColor = Color.White,
        contentColor = Color.Black
    ),
    onClick = {

    },
) {
    Text(text = "重置")
}
Button(
    modifier = Modifier
        .width(100.dp)
        .padding(10.dp),
    colors = ButtonDefaults.buttonColors(
        backgroundColor = colorResource(id = R.color.global_color),
        contentColor = Color.White
    ),
    onClick = {

    },
) {
    Text(text = "确认")
}

Button源码

    fun Button(
        onClick: () -> Unit,
        modifier: Modifier = Modifier,
        enabled: Boolean = true,
        interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
        elevation: ButtonElevation? = ButtonDefaults.elevation(),
        shape: Shape = MaterialTheme.shapes.small,
        border: BorderStroke? = null,
        colors: ButtonColors = ButtonDefaults.buttonColors(),
        contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
        content: @Composable RowScope.() -> Unit
    ) {
        val contentColor by colors.contentColor(enabled)
        Surface(
            modifier = modifier,
            shape = shape,
            color = colors.backgroundColor(enabled).value,
            contentColor = contentColor.copy(alpha = 1f),
            border = border,
            elevation = elevation?.elevation(enabled, interactionSource)?.value ?: 0.dp,
            onClick = onClick,
            enabled = enabled,
            role = Role.Button,
            interactionSource = interactionSource,
            indication = rememberRipple()
        ) {
            CompositionLocalProvider(LocalContentAlpha provides contentColor.alpha) {
                ProvideTextStyle(
                    value = MaterialTheme.typography.button
                ) {
                    Row(
                        Modifier
                            .defaultMinSize(
                                minWidth = ButtonDefaults.MinWidth,
                                minHeight = ButtonDefaults.MinHeight
                            )
                            .padding(contentPadding),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically,
                        content = content
                    )
                }
            }
        }
    }