Jetpack 撰写框中的宽度和高度相等

May*_*jra 26 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}")
                        }
                    }
                }
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

它给了我以下结果:

棋盒布局

我期望它看起来像什么:

棋盒期望

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

Fra*_*esc 65

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}")
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你至少节省了我 8 个工时:) (2认同)
  • 如果宽度上的空间大于高度上的空间,那么这会表现得很奇怪。(底部的瓷砖被切掉) (2认同)

Gab*_*tti 9

您可以使用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}")
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在你的情况下,如果你知道板的宽度,你也可以使用类似的东西:

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)
                        ,
                    )
             //...
                
    }
Run Code Online (Sandbox Code Playgroud)