
我想做一个类似物流的一个页面类似下图,但是左边那条绿线无法显示出来。(代码和图片在后面)
Row(modifier = Modifier.height(IntrinsicSize.Min)) 并且把图片 grid 注释掉,那么绿线是正常显示的Row(modifer = Modifier.fillMaxHeight)并且设置 grid 的 height ,也无法显示 
@Composable fun ListDemo() { var titles = listOf("1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2") LazyColumn(modifier = Modifier .fillMaxSize()) { itemsIndexed(titles) { index, item -> Surface(modifier = Modifier .fillMaxWidth()) { Column(modifier = Modifier .padding(horizOntal= 18.dp)) { Row(modifier = Modifier .padding(vertical = 8.dp), verticalAlignment = Alignment.CenterVertically) { Box(modifier = Modifier .size(24.dp) .background(Color.Red)) } Row(modifier = Modifier .fillMaxWidth()) { Box(modifier = Modifier .width(24.dp) .fillMaxHeight(IntrinsicSize.Min), cOntentAlignment= Alignment.Center){ // TODO show this green line Box(modifier = Modifier .fillMaxHeight() .width(1.dp) .background(Color.Green)) } Spacer(Modifier.width(8.dp)) Column(modifier = Modifier .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { Row(modifier = Modifier .fillMaxWidth()) { Text( text = "Time:", fOntSize= 14.sp, color = Color.Gray, modifier = Modifier .width(100.dp) ) Spacer(Modifier.width(4.dp)) Text( text = "2023-03-02 18:34:07", fOntSize= 14.sp, color = Color.Gray ) } val images = listOf("1","2","3","4") LazyVerticalGrid( columns = GridCells.Fixed(3), horizOntalArrangement= Arrangement.spacedBy(6.dp), verticalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.heightIn( max = 1000.dp) ) { items(images) { pic -> Box( modifier = Modifier .aspectRatio(1f) .background(Color.Gray) ) { Text( text = pic ) } } } } } Spacer(Modifier.height(8.dp)) } } } } } @Preview @Composable fun ListDemoPreview() { ListDemo() } 1 vigroid 281 天前 Column( weight = 1f ) , 不要加上 fillMaxWidth()。占据剩余的空间应该用 weight = 1f |
3 qwell 281 天前 Row(modifier = Modifier.height(IntrinsicSize.Min)) { Box(modifier = Modifier .width(24.dp) .fillMaxHeight(), cOntentAlignment= Alignment.Center){ Box(modifier = Modifier .fillMaxHeight() .width(1.dp) .background(Color.Green)) } Spacer(Modifier.width(8.dp)) Column(modifier = Modifier .weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp)) {...} } 这样,有一点是两个 LazyLayout 滑动冲突了 |
4 rrubick OP @qwell #3 不行,还是会报错 ```shell java. lang. IllegalStateException: Asking for intrinsic measurements of SubcomposeLayout layouts is not supported. This includes components that are built on top of SubcomposeLayout, such as lazy lists, BoxWithConstraints, TabRow, etc. To mitigate this: - if intrinsic measurements are used to achieve 'match parent' sizing, consider replacing the parent of the component with a custom layout which controls the order in which children are measured, making intrinsic measurement not needed - adding a size modifier to the component, in order to fast return the queried intrinsic measurement. ``` |
5 jojo0830 281 天前 动态获取 Column 的高度吧。 Column( modifier = Modifier .fillMaxWidth() .onSizeChanged { sizeInDp = with(density) { it.width.toDp() to it.height.toDp() } }, verticalArrangement = Arrangement.spacedBy(8.dp) ), 然后把高度给到 box 。 Box( modifier = Modifier .width(24.dp) .height(sizeInDp.second) .background(Color.Red), cOntentAlignment= Alignment.Center ) |
8 od66666666 281 天前 遇 size 不决,直接 onGloballyPositioned Row(modifier = Modifier.fillMaxWidth()) { val density = LocalDensity.current var columnHeight by remember { mutableStateOf(0.dp) } Box( modifier = Modifier .height(columnHeight) .width(24.dp), cOntentAlignment= Alignment.Center ) { ... } Spacer(Modifier.width(8.dp)) Column(modifier = Modifier .onGloballyPositioned { layoutCoordinates -> columnHeight = with(density) { layoutCoordinates.size.height.toDp() } } .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { ... } } |
9 lisongeee 281 天前 上面报错已经说得很明白了,你的子布局有无法确定 size 的组件 LazyVerticalGrid LazyXXX 就是前端概念里的虚拟列表,必须要一个容器高度才能计算虚拟滚动 或者换成自定义 Column{Row{}} |
10 zeropercenthappy 281 天前 从你的布局来看,内部的列表应该没有必要使用 LazyVerticalGrid ,这里应该是 Lazy 控件导致的高度计算问题。考虑直接换成 FlowRow 吧。 |
11 leiiiooo 281 天前 使用 Canvas 自定义吧,我也试了半天 |
12 lisongeee 281 天前 https://gist.github.com/lisonge/b689ea150c7df8ae2618c4f73203ad9e 整了半天,发现在外面套个 box 就行,代码和运行截图都在上面的 gist 里了 |
13 rrubick OP @jojo0830 #5 您这个 density 从哪来的? import androidx.compose.ui.draw.EmptyBuildDrawCacheParams.density 一直报错,`Cannot access 'EmptyBuildDrawCacheParams': it is private in file` |
15 jojo0830 280 天前 @magic3584 #13 顶层创建一个呗: val density = LocalDensity.current var sizeInDp by remember { mutableStateOf(0.dp to 0.dp) } |
17 rrubick OP @od66666666 #8 感谢,您这个跟 5 楼的差不多 |