Jetpack 组合框中的等宽和高度

时间:2021-05-26 16:06:22

标签: android android-jetpack-compose

所以我正在尝试使用 JetPack compose 创建一个国际象棋棋盘并设法绘制了一些布局。但是,现在我有一个问题,我希望棋盘正方形的高度与宽度相等。我希望它是完美的正方形。

注意:我不想给出任何固定的宽度或高度。我想在屏幕宽度中放置正方形,然后每个正方形的高度应该与宽度一样多。

我尝试过的:

@ExperimentalFoundationApi
class PlayGameActivity : BaseActivity() {
    val darkSquare = Color(0xFF779556)
    val lightSquare = Color(0xFFEBECD0)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Column {
                Board()
            }
        }
    }

    @Composable
    fun Board() {
        Column {
            for (i in 0 until 8) {
                Row {
                    for (j in 0 until 8) {
                        val isLightSquare = i % 2 == j % 2
                        val squareColor = if (isLightSquare) lightSquare else darkSquare
                        Box(
                            modifier = Modifier
                                .weight(1f)
                                .background(squareColor)
                        ) {
                            Text(text = "${i + j}")
                        }
                    }
                }
            }
        }
    }

}

它给了我以下结果。

Chess Box Layout

我期望的样子:

Chess Box Expectations

知道如何计算宽度并将其设置为与正方形的宽度相同吗?

2 个答案:

答案 0 :(得分:3)

您可以使用 layout 修饰符来调整每个 Box 的维度。

类似于:

Column {
    for (i in 0 until 8) {
        Row {
            for (j in 0 until 8) {
                val isLightSquare = i % 2 == j % 2
                val squareColor = if (isLightSquare) Yellow else Red
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .background(squareColor)

                        .layout(){ measurable, constraints ->
                            // Measure the composable
                            val placeable = measurable.measure(constraints)

                            //get the current dimension to assign width=height
                            val currentWidth = placeable.width
                            
                            //assign the dimension and the position
                            layout(currentWidth, currentWidth) {
                                // Where the composable gets placed
                                placeable.placeRelative(0, 0)
                            }
                        }
                    ,
                ) {
                    Text(text = "${i + j}")
                }
            }
        }
    }
}

enter image description here

在您的情况下,如果您知道板宽,您还可以使用以下内容:

var size by remember { mutableStateOf (Size.Zero) }
val width = with(LocalDensity.current){
        (size.width/8).toDp()
}
Column(
        Modifier.fillMaxSize()
            .onGloballyPositioned { coordinates ->
              size = coordinates.size.toSize()
    }) {
             //...
                    Box(
                        modifier = Modifier
                            .size(
                                width = width,
                                height = width,
                            )
                            .background(squareColor)
                        ,
                    )
             //...
                
    }

答案 1 :(得分:2)

更简单的解决方案是在修饰符上设置 aspectRatio 属性:

@Composable
fun Board() {
    Column {
        for (i in 0 until 8) {
            Row {
                for (j in 0 until 8) {
                    val isLightSquare = i % 2 == j % 2
                    val squareColor = if (isLightSquare) lightSquare else darkSquare
                    Box(
                        modifier = Modifier
                            .weight(1f)
                            .aspectRatio(1f)
                            .background(squareColor)
                    ) {
                        Text(text = "${i + j}")
                    }
                }
            }
        }
    }
}