我正在尝试使 Imagocosable
根据其内容包装其高度和宽度,以及两个 text
可复合,与 coptement 可理解。以下是为此的代码:
@Composable
fun ImageComposable(url:String){
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current).data(url).apply{
placeholder(drawableResId = R.drawable.ic_broken_pic)
}.build()
)
Image(painter = painter, contentDescription = null, Modifier.padding(2.dp).border(width = 2.dp, shape = CircleShape, color = MaterialTheme.colors.onPrimary)
}
@Composable
fun Assemble(url:String){
Column (modifier = Modifier.fillMaxWidth().height(400.dp).background(MaterialTheme.colors.primary)
.padding(16.dp), verticalArrangement = Arrangement.Bottom) {
ImageComposable(url)
Text(text = "title")
Text(text = "Body")
}
}
但是 Imagecosable
最终取得了组装
composable的所有高度和宽度,我看不到两个 text 我在列
中添加的组件。因此,我对这里的确切问题感到困惑。我认为至少应该显示 Imagecosable
以及两个 text
可复合,但没有发生。
我正在此处使用 coil
图像加载库以从URL解析图像。现在,在测试中,我将URL作为空字符串
传递。因此,我将组合称为:
组装(“”)
我没有找到任何可以帮助我理解这种行为的文档。因此,我想知道这个问题的原因以及解决问题的可能解决方案。
I am trying to make the ImageComposable
wrap its height and width according to its content, along with the two Text
composable, align to the bottom of Assemble
composable. Following is the code for that:
@Composable
fun ImageComposable(url:String){
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current).data(url).apply{
placeholder(drawableResId = R.drawable.ic_broken_pic)
}.build()
)
Image(painter = painter, contentDescription = null, Modifier.padding(2.dp).border(width = 2.dp, shape = CircleShape, color = MaterialTheme.colors.onPrimary)
}
@Composable
fun Assemble(url:String){
Column (modifier = Modifier.fillMaxWidth().height(400.dp).background(MaterialTheme.colors.primary)
.padding(16.dp), verticalArrangement = Arrangement.Bottom) {
ImageComposable(url)
Text(text = "title")
Text(text = "Body")
}
}
but the ImageComposable
ends up taking all the height and width of the Assemble
composable and I am not able to see the two Text
composables that I added in the column
. So I am confused as to what is the exact problem here. I thought at least it should show the ImageComposable
along with the two Text
composable but it is not happening.
I am using coil
image loading library here for parsing the image from url. For now in testing, I am passing url as an Empty String
. Hence I am calling the composable as:
Assemble("")
I didn't find any document that would help me understand this behavior. So I wanted to know the reason to this problem and possible solutions to overcome it.
发布评论
评论(2)
如果有您想要实现的目标的草图,那么解决问题将更容易。
尽管如此,我希望我能提供帮助:
看来您面临的问题可以通过“ nofollow noreferrer”> instossic intinsic insinsic测量/a>。
该列分别测量每个孩子,而没有文本的尺寸约束图像大小。对于此
内在
可以使用。通过使用
intrinsicsize.max
用于width
的组装
composoble as ass as ass as ass:您可以创建这样的布局:
(请注意,我在此处使用本地绘制)
现在您可以看到2个文本,并将图像的宽度调整为文本的宽度。
使用
Interins
来衡量彼此依赖的儿童应帮助您实现所需的目标。请让我知道该布局是否不符合您的期望。
It would be easier to solve your problem if there would be a sketch of what you want to achieve.
Nevertheless, I hope I can help:
It looks like the issue you are facing can be handled by Intrinsic measurements in Compose layouts.
The column measures each child individually without the dimension of your text constraining the image size. For this
Intrinsics
can be used.By using
IntrinsicSize.Max
for thewidth
of theAssemble
composable like this:you can can create a layout like this:
(Please note that I am using a local drawable here)
You can now see the 2 texts and the width of the image is adjusted to the width of the texts.
Using
Intrinsics
to measure children in dependance to each other should help you to achieve what you wanted.Please let me know if this layout does not meet your expectations.
您可以明确指定每个组件的高度:
或者您可以指定将要占用的最大高度的一小部分:
您也可以尝试使用“重量修饰符:”:
You can explicitly specify the height of each component:
Or you can specify a fraction of the maximum height it will take up:
You can also try playing with the weight modifier: