gpt4 book ai didi

android - Jetpack Compose 中的 MaterialButtonToggleGroup

转载 作者:行者123 更新时间:2023-12-04 23:40:45 27 4
gpt4 key购买 nike

我想实现 MaterialButtonToggleGroup在 Jetpack Compose 中。该组件如下所示(图片取自 here):

到目前为止,我得到了以下结果:

请注意,存在垂直蓝色边框旁边的垂直灰色边框。在原版中,一次出现彩色边框或灰色。为了更清楚,看看这张带有超厚边框的图片:

如何实现两个按钮之间的垂直边框不存在?我当前的代码如下所示:

    val cornerRadius = 8.dp

Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Spacer(modifier = Modifier.weight(1f))

items.forEachIndexed { index, item ->
OutlinedButton(
onClick = { indexChanged(index) },
shape = when (index) {
// left outer button
0 -> RoundedCornerShape(topStart = cornerRadius, topEnd = 0.dp, bottomStart = cornerRadius, bottomEnd = 0.dp)
// right outer button
items.size - 1 -> RoundedCornerShape(topStart = 0.dp, topEnd = cornerRadius, bottomStart = 0.dp, bottomEnd = cornerRadius)
// middle button
else -> RoundedCornerShape(topStart = 0.dp, topEnd = 0.dp, bottomStart = 0.dp, bottomEnd = 0.dp)
},
border = BorderStroke(1.dp, if(selectedIndex == index) { MaterialTheme.colors.primary } else { Color.DarkGray.copy(alpha = 0.75f)}),
colors = if(selectedIndex == index) {
// selected colors
ButtonDefaults.outlinedButtonColors(backgroundColor = MaterialTheme.colors.primary.copy(alpha = 0.1f), contentColor = MaterialTheme.colors.primary)
} else {
// not selected colors
ButtonDefaults.outlinedButtonColors(backgroundColor = MaterialTheme.colors.surface, contentColor = MaterialTheme.colors.primary)
},
) {
Text(
text = "Some text",
color = if(selectedIndex == index) { MaterialTheme.colors.primary } else { Color.DarkGray.copy(alpha = 0.9f) },
modifier = Modifier.padding(horizontal = 8.dp)
)
}
}

Spacer(modifier = Modifier.weight(1f))
}

最佳答案

MaterialButtonToggleGroup 为了防止双倍宽度的笔画,除了第一个 child 直接在彼此之上绘制相邻笔画之外,所有其他人都有一个负的 marginStart。
使用相同的解决方案:

OutlinedButton(
modifier = when (index) {
0 ->
Modifier
.offset(0.dp, 0.dp)
.zIndex(if (selectedIndex == index) 1f else 0f)
else ->
Modifier
.offset((-1 * index).dp, 0.dp)
.zIndex(if (selectedIndex == index) 1f else 0f)
},
// Your code here
enter image description here
enter image description here

关于android - Jetpack Compose 中的 MaterialButtonToggleGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67023923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com