我正在使用
KamelImage
将图像加载到 LazyRow。但是,当图像加载失败时,我会显示错误图像。然而,问题是这个错误图像永远不会与成功图像的高度相同。
有没有办法让它们统一,这样所有图像的高度都相同。
Card(
modifier = modifier
.fillMaxHeight()
.clickable {
onCastClicked(cast.id)
},
elevation = 2.dp
) {
Column(
modifier = Modifier.padding(top = 4.dp, bottom = 10.dp, start = 6.dp, end = 6.dp),
verticalArrangement = Arrangement.Top,
) {
KamelImage(
resource = { asyncPainterResource(data = cast.profilePath) },
contentDescription = cast.name,
modifier = Modifier
.aspectRatio(16f/9f)
.align(Alignment.CenterHorizontally),
contentScale = ContentScale.Crop,
contentAlignment = Alignment.Center,
onLoading = { _ ->
CircularProgressIndicator(
modifier = Modifier.align(Alignment.Center),
color = Color.Blue
)
},
onFailure = {
Box(
modifier.aspectRatio(16f/9f)
) {
Image(
modifier = Modifier.fillMaxWidth(),
painter = painterResource(Res.drawable.person),
contentDescription = "Fall back image"
)
}
it.printStackTrace()
},
)
Spacer(modifier = Modifier.height(6.dp))
Text(
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Start,
text = cast.name,
fontWeight = FontWeight.Bold,
fontSize = 14.sp
)
Text(
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Start,
text = cast.character,
fontSize = 14.sp
)
}
}
您的
onFailure
Image
具有 fillMaxWidth
修饰符,应将其替换为 fillMaxSize
,以便它可以与其他图像配合。您还可以将 contentScale
设置为 ContentScale.Crop
或 ContentScale.FillBounds
以填充 Card
的宽度。