英文:
Jetpack Compose: How to change color of a button border depending on if it is enabled or disabled?
问题
是否可以在禁用时更改边框的颜色 .border(
?
类似于使用 ButtonDefaults.buttonColors
来将边框颜色设置为 Color.Gray
,如果按钮被禁用,并保持背景透明。
英文:
Is it possible to change the color of the border .border(
when it is disabled?
Something like ButtonDefaults.buttonColors
to make border color Color.Gray
if the button is disabled and keeping background transparent.
@Composable
fun MyButton(
modifier: Modifier = Modifier,
title: String,
isEnabled: Boolean = true,
onClick: () -> Unit
) {
Button(
modifier = modifier
.height(40.dp)
.border(
width = 1.dp,
color = Color.Red,
shape = RoundedCornerShape(16.dp)
)
.background(
color = Color.Transparent,
shape = RoundedCornerShape(16.dp),
),
shape = RoundedCornerShape(16.dp),
onClick = onClick,
elevation = ButtonDefaults.elevation(
defaultElevation = 0.dp,
pressedElevation = 0.dp,
hoveredElevation = 0.dp
),
colors = ButtonDefaults.buttonColors(
backgroundColor = Color.Transparent,
disabledBackgroundColor = Color.Transparent,
contentColor = Color.White,
disabledContentColor = Color.Gray
),
enabled = isEnabled
) {
Text(text = title)
}
}
答案1
得分: 2
Sure, here's the translated code:
只需在 border
修饰符的 color
属性中添加一个条件。
类似于:
.border(
width = 1.dp,
color = if (isEnabled) Color.Red else Color.Gray,
shape = RoundedCornerShape(16.dp)
)
英文:
Just add a condition in the color
attribute in the border
modifier.
Something like:
.border(
width = 1.dp,
color = if (isEnabled) Color.Red else Color.Gray,
shape = RoundedCornerShape(16.dp)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论