kotlin - Jetpack Compose:使父级之外的偏移图像可见
问题描述
我在带有背景颜色的卡片内添加了一个图像,我希望我的图像的某些部分溢出容器之外并且可见。我曾经offset
偏移图像,但我找不到让它可见的方法。
这是我的代码:
Card(
shape = RoundedCornerShape(8.dp),
modifier = Modifier.padding(),
backgroundColor = Color.Cyan,
onClick = {},
) {
Box(modifier = Modifier.wrapContentHeight()) {
Image(
modifier = Modifier
.width(250.dp)
.align(alignment = Alignment.TopEnd)
.offset(y = -75.dp, x = 50.dp),
painter = painterResource(id = R.drawable.ic_pray),
contentDescription = null,
)
Column(
) {}
}
}
解决方案
Card
基于材质表面,它使用 Modifier.clip
它来防止显示您的偏移视图。
如果需要保持Card
高度,可以将图像放置在Box
外部Card
:
Box {
Card(...)
Image(
modifier = Modifier
.width(250.dp)
.align(alignment = Alignment.TopEnd)
.offset(y = -75.dp, x = 50.dp),
painter = painterResource(id = R.drawable.my_image),
contentDescription = null,
)
}
否则,您可以Card
首先跳过使用,并将背景和形状设置为您的Box
with Modifier.background
:
Box(
modifier = Modifier
.padding(10.dp)
.background(Color.Cyan, shape = RoundedCornerShape(8.dp))
) {
Box(modifier = Modifier.wrapContentHeight()) {
Image(
modifier = Modifier
.width(250.dp)
.align(alignment = Alignment.TopEnd)
.offset(y = -75.dp, x = 50.dp),
painter = painterResource(id = R.drawable.my_image),
contentDescription = null,
)
Column(
) {}
}
}
推荐阅读
- vue.js - Vue.js:在 js 文件中获取环境变量
- c# - 使用 Graphics.DrawString 在 C# 中打印长字符串
- python-3.x - 无法在 PowerBI 中使用 LabelEncoder pickle 文件
- css - Flexbox wrap - 第一个和最后一个项目在 iPad 上显示不正确
- angular - 异步管道是否取消订阅服务中定义的可观察对象并指向组件变量?
- c# - C#,EF Core 3,并发,如何正确实现对 SQL 数据库的写入/读取操作的并行处理?
- javascript - 如果数组项具有重复的 id,则删除它
- python - fileDialog 的 qt5 主窗口为空
- python - 在 Python 中高效地创建 Numpy 数组
- javascript - AngularJS Scope 无法通过控制台访问