我想将按钮对齐到行中盒子的BottomEnd(在下面的卡片的BottomEnd)。我有一个带有两个部分的行(Card和Box)的卡片,并且我想让盒子填充剩余行的最大值。我无法按照自己的想法实现它。下面是当前代码的可视化效果。
@Composable
fun ProductItem(product: ProductModel, onItemClick: () -> Unit, onAddToCardButton: () -> Unit) {
Card(
modifier = Modifier
.fillMaxSize()
.padding(7.dp)
.clickable { onItemClick() },
shape = MaterialTheme.shapes.large,
elevation = 4.dp
) {
Row(
modifier = Modifier.fillMaxSize()
) {
Card(
modifier = Modifier
.weight(1f),
shape = MaterialTheme.shapes.small,
elevation = 2.dp
) {
Image(
painter = painterResource(id = R.drawable.ic_splash_screen),
contentDescription = "Image of ${product.name}",
)
}
Box(
modifier = Modifier
.weight(2f)
.fillMaxHeight()
.padding(6.dp)
.background(Color.Green)
) {
Column(modifier = Modifier.align(Alignment.TopStart)) {
Text(
text = "${product.number}. ${product.name}",
fontWeight = FontWeight.Bold,
style = MaterialTheme.typography.h4,
)
Text(
text = product.ingredients, fontStyle = FontStyle.Italic
)
}
Button(
modifier = Modifier.align(Alignment.BottomEnd),
onClick = {
onAddToCardButton()
},
shape = RoundedCornerShape(8.dp),
) {
if (product.type == "pizza") {
Text(text = "od ${String.format("%.2f", product.price[0])} zł")
} else {
Text(text = "${String.format("%.2f", product.price[0])} zł")
}
}
}
}
}
}
ProductItem(...,cardHeight: Int = 130, ...) { Card(modifier = ....requiredHeight(cardHeight.dp)
。也许有一些误解:如果您在Row内部的子元素中编写.weight()
,它不会影响子元素的高度,只会影响宽度。(如果您在Column内部编写它,则只会影响子元素的高度而不是宽度)。 - Abokyy