简体   繁体   English

尝试在 Jetpack Compose 中实现键盘布局

[英]Trying to achieve keyboard layout in Jetpack Compose

I am trying to achieve a normal keyboard layout like in this picture我正在尝试实现像这张图片中那样的正常键盘布局

在此处输入图像描述

However, in the past 3 hours, I've been stuck with this result and I don't know how I am supposed to replicate this, maybe I'm missing something.然而,在过去的 3 个小时里,我一直坚持这个结果,我不知道我应该如何复制它,也许我遗漏了什么。 The keys need to have the same width and height and the rows with a smaller number of letters will be centered.键需要具有相同的宽度和高度,并且字母数量较少的行将居中。

在此处输入图像描述

Right now my code looks like this:现在我的代码看起来像这样:

@Composable
fun GameKeyboard() {

    val firstRow = listOf("Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P")
    val secondRow = listOf("A", "S", "D", "F", "G", "H", "J", "K", "L")
    val thirdRow = listOf("Z", "X", "C", "V", "B", "N", "M")

    Column(
        modifier = Modifier.fillMaxWidth(),
        verticalArrangement = Arrangement.spacedBy(4.dp)
    ) {
        GameKeyboardRow(firstRow)
        GameKeyboardRow(secondRow)
        GameKeyboardRow(thirdRow)
    }
}

@Composable
fun GameKeyboardRow(keyList: List<String>) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.spacedBy(2.dp)
    ) {
        keyList.forEach {
            GameKeyboardKey(it, Modifier.weight(1f, true))
        }
    }
}

@Composable
fun GameKeyboardKey(key: String, modifier: Modifier) {

    Box(contentAlignment = Alignment.Center,
        modifier = modifier
            .aspectRatio(1f)
            .border(
                width = 2.dp,
                color = MaterialTheme.colorScheme.outline,
                shape = MaterialTheme.shapes.small
            )
            .clickable {

            }) {
        Text(
            text = key,
            textAlign = TextAlign.Center
        )
    }
}

Nvm, I found the answer myself, I will post it here if anybody else is looking for it, sometimes the answer is right in front of your eyes. Nvm,我自己找到了答案,如果有人正在寻找它,我会把它贴在这里,有时答案就在你眼前。

I ended up counting letters and realizing I can use the weight to space around the second and third rows.我最终数了数字母,并意识到我可以使用重量在第二行和第三行周围留出空间。

if the first row has n letters, then the second row was n-1 letters so in the Row view, I will have two Spacer views that will take the weight / 2 .如果第一行有n字母,那么第二行是n-1个字母,所以在Row视图中,我将有两个Spacer视图,它们的weight / 2 Same logic applies for third row which has n-3 letters相同的逻辑适用于具有n-3个字母的第三行

    GameKeyboardRow(firstRow)
    GameKeyboardRow(secondRow, 1f)
    GameKeyboardRow(thirdRow, 3f)

And then the Row looks like this:然后Row看起来像这样:

Row(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.spacedBy(2.dp)
) {
    if (marginWeight != null) {
        Spacer(modifier = Modifier.weight(marginWeight / 2))
    }

    keyList.forEach {
        GameKeyboardKey(it, Modifier.weight(1f, true))
    }

    if (marginWeight != null) {
        Spacer(modifier = Modifier.weight(marginWeight / 2))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM