Android 等宽度&;Jetpack组合框中的高度
所以我尝试用JetPack compose创建一个棋盘&我已经设法画了一点布局。但是,现在我有一个问题,我希望板正方形的高度和宽度相等。我希望它是完美的正方形 注意:我不想给出任何固定宽度或高度。我想在屏幕宽度中加入正方形&然后每个正方形的高度应该与宽度一样大 我所尝试的:Android 等宽度&;Jetpack组合框中的高度,android,android-jetpack-compose,Android,Android Jetpack Compose,所以我尝试用JetPack compose创建一个棋盘&我已经设法画了一点布局。但是,现在我有一个问题,我希望板正方形的高度和宽度相等。我希望它是完美的正方形 注意:我不想给出任何固定宽度或高度。我想在屏幕宽度中加入正方形&然后每个正方形的高度应该与宽度一样大 我所尝试的: @ExperimentalFoundationApi class PlayGameActivity : BaseActivity() { val darkSquare = Color(0xFF779556)
@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}")
}
}
}
}
}
}
}
它给了我以下的结果
我所期望的是:
知道如何计算宽度并将其设置为正方形的宽度吗?您可以使用修改器调整每个框的尺寸
比如:
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}")
}
}
}
}
}
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)
,
)
//...
}
在您的情况下,如果您知道电路板的宽度,您也可以使用以下内容:
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}")
}
}
}
}
}
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)
,
)
//...
}
您可以使用修改器调整每个框的尺寸
比如:
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}")
}
}
}
}
}
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)
,
)
//...
}
在您的情况下,如果您知道电路板的宽度,您也可以使用以下内容:
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}")
}
}
}
}
}
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)
,
)
//...
}
更简单的解决方案是在修改器上设置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}")
}
}
}
}
}
}
更简单的解决方案是在修改器上设置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}")
}
}
}
}
}
}