繁体   English   中英

ConstraintLayout 与 Jetpack Compose 中的 LazyRow 不兼容

[英]ConstraintLayout doesn't quite work with LazyRow in Jetpack Compose

我想将Image的高度限制为LazyRow的高度,并将每个图像并排对齐。 我能够通过这段代码实现大部分布局:

package com.example.test

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.ConstraintLayout
import androidx.compose.foundation.layout.Dimension
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.unit.dp



@Composable
fun Test() {
    ConstraintLayout {
        val (image, row) = createRefs()
        createVerticalChain(image, row)
        ARow(modifier = Modifier.constrainAs(row) {
            start.linkTo(image.end)
            width = Dimension.fillToConstraints
        })

        AImage(modifier = Modifier.constrainAs(image) {
            top.linkTo(row.top)
            bottom.linkTo(row.bottom)
            height = Dimension.fillToConstraints
        })
    }
}


@Composable
fun AImage(modifier: Modifier = Modifier) {
    Image(
        modifier = modifier,
        bitmap = imageResource(id = R.drawable.sample_ic),
        )
}

@Composable
fun ARow(modifier: Modifier = Modifier) {
    LazyRow(modifier = modifier) {
        items((0..10).toList()) {
            Text(modifier = Modifier.padding(8.dp), text = "Item: $it")
        }
    }
}

但是我有一个小问题。 LazyRow切断最后一个元素:

例子

尽管使用width = Dimension.fillToConstraints和链条,但仍会出现此问题。 如果你想克隆,是 repo

将您的ConstraintLayout设置为最大宽度,并向您的ARow添加一个结束约束:

ConstraintLayout(modifier = Modifier.fillMaxWidth()) {
    val (image, row) = createRefs()
    createVerticalChain(image, row)
    ARow(modifier = Modifier.constrainAs(row) {
        start.linkTo(image.end)
        end.linkTo(parent.end)
        width = Dimension.fillToConstraints
    })

    AImage(modifier = Modifier.constrainAs(image) {
        top.linkTo(row.top)
        bottom.linkTo(row.bottom)
        height = Dimension.fillToConstraints
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM