我想更改复选框中的图标。我们可以使用 android:button="@drawable/selector_checkbox"
在 android 中更改它,但我没有找到任何直接的解决方案来在撰写中更改它。
我可以通过像这个答案一样重写复选框来更改它,但这不是一个完美的解决方案: https://stackoverflow.com/a/67363006/1105277
在 Jetpack Compose 中是否有更好的方法来做到这一点?
最佳答案
我想出了一个简单的自定义复选框解决方案,它对我来说非常有用。这不是一个万能的解决方案,您应该对其进行修改以满足您的需求。
这使用两个图标/xml R.drawable.checkbox_off_icon
作为框架,使用 R.drawable.checkbox_on_icon
作为检查(不带框)
@Composable
fun CustomCheckbox(
modifier: Modifier,
checked: Boolean,
onCheckedChange: (Boolean) -> Unit
) {
IconButton(modifier = modifier, onClick = { onCheckedChange(!checked) }) {
//the box image frame unchecked
Image(
painter = painterResource(id = R.drawable.checkbox_off_icon),
contentDescription = "Unchecked"
)
AnimatedVisibility(
modifier = modifier,
visible = checked,
exit = shrinkOut(shrinkTowards = Alignment.TopStart) + fadeOut()
) {
//the check only (without the surrounding box)
Image(
painter = painterResource(id = R.drawable.checkbox_on_icon),
contentDescription = "Checked"
)
}
}
}
关于android - 如何更改撰写中的复选框图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71078243/