【问题标题】:MaterialButtonToggleGroup in Jetpack ComposeJetpack Compose 中的 MaterialButtonToggleGroup
【发布时间】:2021-04-09 15:24:14
【问题描述】:

我想在 Jetpack Compose 中实现 MaterialButtonToggleGroup。这个组件长这样(图片取自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))
    }

【问题讨论】:

    标签: android kotlin android-button android-jetpack-compose modifier


    【解决方案1】:

    MaterialButtonToggleGroup 中,为了防止双倍宽度的笔划,除了第一个孩子直接在彼此之上绘制相邻笔划之外,所有的marginStart 都有一个负值。

    使用相同的解决方案:

       OutlinedButton(
                    modifier = when (index){
                        0 -> {
                            if (selectedIndex == index) {
                                Modifier.offset(0.dp, 0.dp).zIndex(1f)
                            } else {
                                Modifier.offset(0.dp, 0.dp).zIndex(0f)
                            }
                        }
                        else -> {
                            val offset = -1 * index
                            if (selectedIndex == index) {
                                Modifier.offset(offset.dp, 0.dp).zIndex(1f)
                            } else {
                                Modifier.offset(offset.dp, 0.dp).zIndex(0f)
                            }
                        }
                    },
              //.. your code
       )
    

    【讨论】:

    • 工作得很好,非常感谢!我什至不知道有一个 zIndex 修饰符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2022-11-10
    • 2023-01-22
    • 2021-12-26
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多