android - Jetpack 撰写 : How to make two columns equals height

标签 android kotlin android-jetpack-compose

其中一行两列,左列有两个子行,右列有子行。

enter image description here

这是我的代码:

@Preview(showBackground = true)
    @Composable
    fun TwoColumns() {
        Row {
            BottomLeftLetterKey(columns = 2, rows = 2, Color.Blue)
            Spacer(modifier = Modifier.weight(1f))
            BottomLeftLetterKey(columns = 3, rows = 3, Color.Cyan)
        }
    }

    @Composable
    fun BottomLeftLetterKey(columns: Int, rows: Int, color: Color) {
        Column(modifier = Modifier.background(color)) {
            repeat(columns) {
                Row {
                    repeat(rows) {
                        ElevatedButton(onClick = { /*TODO*/ },
                            shape = RoundedCornerShape(4.dp),
                            colors = ButtonDefaults.elevatedButtonColors(containerColor = Color.Green, contentColor = Color.Red),
                            contentPadding = PaddingValues(0.dp)
                        ) {
                            Text(text = "2")
                        }
                    }
                }
            }
        }
    }

我希望两列高度相同。即蓝色柱应与青色柱高度相同。

我尝试设置: Column(modifier = Modifier.background(color).fillMaxHeight())

那么我想要的高度太高了。

enter image description here

有什么建议吗?谢谢!

编辑

modifier = modifier.height(IntrinsicSize.Min/Max) 是正确的方向。但困惑 MinMax 哪个选项是正确的?我试过了,效果是一样的。

来自doc :

The Row composable's minIntrinsicHeight will be the maximum minIntrinsicHeight of its children.

似乎 IntrinsicSize.Min 是正确的,而不是 IntrinsicSize.Max

最佳答案

您必须将 Modifier.height(IntrinsicSize.Min) 应用于 Row,并将 fillMaxHeight() 修饰符应用于

Row(
    modifier = Modifier.height(IntrinsicSize.Min),
    horizontalArrangement = Arrangement.SpaceBetween
) {
    BottomLeftLetterKey(columns = 2, rows = 2, Color.Blue)
    BottomLeftLetterKey(columns = 3, rows = 3, Color.Cyan)
}


@Composable
fun BottomLeftLetterKey(columns: Int, rows: Int, color: Color) {
    Column(modifier = Modifier
        .fillMaxHeight()  //<--- fill max height
        .background(color)
    ) {
        //....
    }
}

enter image description here

关于android - Jetpack 撰写 : How to make two columns equals height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76089598/

相关文章:

Android 多屏限定符定义

Kotlin:更新不可变列表元素

android - Gradle 同步错误 : Gradle DSL method not found: 'implementation()'

android - 如何使用 Gradle 创建 OBB 文件

android - Proguard 和 HashMap<String, MyObject>

android - 在 Handler 和 Thread Widget 中设置 TextView

android - 撰写中的主题相关资源

kotlin - Kotlin 中的保留关键字是什么?

android - lambda 函数中的类型不匹配 - Kotlin

android - 如何正确地将图像上传到 Jetpack Compose 中的 LazyList 中的项目?