在我的 Compose 應用程式中,我需要創建一個圓形復選框。我已經用下面的代碼實作了這一點:
@Composable
fun CircleCheckBox(
isChecked: Boolean,
modifier: Modifier = Modifier,
onChecked: () -> Unit = {},
checkedBackgroundColor: Color,
unCheckedBackgroundColor: Color,
checkedIconColor: Color,
unCheckedIconColor: Color
) {
Box(
modifier = modifier
.clip(CircleShape)
.clickable { onChecked() }
.border(
width = if (!isChecked) 1.dp else 0.dp,
color = if (!isChecked) checkedBackgroundColor else Color.Transparent,
shape = CircleShape
)
.background(
color = if (isChecked) checkedBackgroundColor else unCheckedBackgroundColor,
shape = CircleShape
),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Default.Check,
contentDescription = stringResource(R.string.icon_check),
modifier = Modifier.padding(3.dp),
tint = if (isChecked) checkedIconColor else unCheckedIconColor
)
}
}
但是在我的應用程式中,卡片上有漸變背景,所以我想讓復選標記透明,但在這種實作中,由于 Box 的背景,這是不可能的。有什么方法可以實作它,如下圖所示?


uj5u.com熱心網友回復:
您可以找到合適的默認圖示而不是自己繪制。Icons.Default.CheckCircle就是你要找的 - 它在實心圓圈內有透明的復選標記。您可以使用Icons.Outlined.Circle代替邊框修飾符:
@Composable
fun CircleCheckBox(
isChecked: Boolean,
modifier: Modifier = Modifier,
onChecked: () -> Unit = {},
color: Color,
) {
Box(
contentAlignment = Alignment.Center,
modifier = modifier
.clip(CircleShape)
.clickable { onChecked() }
) {
Icon(
imageVector = if (isChecked) Icons.Default.CheckCircle else Icons.Outlined.Circle,
contentDescription = stringResource(R.string.icon_check),
tint = color
)
}
}
結果:
![]()
uj5u.com熱心網友回復:
我認為您可以通過匯入這樣的自定義圖示矢量來實作這一點

在這里,檢查是透明的,所以它會顯示漸變背景。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/401113.html
標籤:安卓 科特林 复选框 android-jetpack-compose
