使用XML时,我们可以选择使用tools:
进行设计,在实际数据不可用时使用占位符。我们在Jetpack的写作中有类似的东西吗?
我知道我可以通过一个专用的预览函数将示例数据传递给我的可组合数据。但是,例如,当一个图像源是一个URL (通过线圈加载时,幻灯片..。),即使我传递了一个示例URL,它也不能在预览中加载。一个实用的解决方案可以节省开发时间。
发布于 2021-11-01 16:58:55
正如对cd1答案的更新一样:
rememberCoilPainter is renamed to rememberImagePainter and its arguments changed
有关更改的更多信息:
来源:https://coil-kt.github.io/coil/compose/
对于预览中可见的占位符,代码是:
Image(
painter = rememberImagePainter(
data = "https://www.example.com/image.jpg",
builder = {
placeholder(R.drawable.placeholder)
}
),
contentDescription = "some description",
)
发布于 2021-07-12 07:55:47
如果使用的是线圈,则可以使用previewPlaceholder
参数,例如:
Image(
painter = rememberCoilPainter(
request = "https://picsum.photos/300/300",
previewPlaceholder = R.drawable.placeholder,
),
contentDescription = stringResource(R.string.image_content_desc),
)
发布于 2022-03-06 23:47:24
11月20日至22日
如果你用线圈..。
implementation("io.coil-kt:coil-compose:2.2.2")
..。现在,您可以在调试模式下使用占位符进行预览,只有这样:
AsyncImage(
model = "https://www.example.com/image.jpg",
placeholder = debugPlaceholder(R.drawable.debugPlaceholder),
contentDescription = stringResource(R.string.description)
)
并创建一个util方法:
@Composable
fun debugPlaceholder(@DrawableRes debugPreview: Int) =
if (LocalInspectionMode.current) {
painterResource(id = debugPreview)
} else {
null
}
您可以在这里看到更多信息:https://coil-kt.github.io/coil/compose/
https://stackoverflow.com/questions/68343581
复制相似问题