简体   繁体   English

是否可以让 LazyRow/LazyColumn 在屏幕上显示一定数量的项目而不传递明确的 DP 宽度

[英]Is it possible to make LazyRow/LazyColumn display a certain amount of items on the screen without passing in an explicit DP width

Is there a way to make a LazyRow / Row display for example 2 items on the screen simultaneously without measuring the screen width manually and passing the DP width to the children of the LazyRow ?有没有办法让LazyRow / Row同时在屏幕上显示 2 个项目,而无需手动测量屏幕宽度并将 DP 宽度传递给LazyRow的子项?

Current setup example:当前设置示例:

LazyRow(
    modifier = Modifier
        .fillMaxWidth()
        .height(200.dp)
) {
    item {
        FirstItem(
            modifier = Modifier.width(
                ScreenUtils.getScreenWidthDP(LocalContext.current) / columnCount
            )
        )
    }
    item {
        SecondItem(
            modifier = Modifier.width(
                ScreenUtils.getScreenWidthDP(LocalContext.current) / columnCount
            )
        )
        )
    }
}

You can use Modifier.fillParentMaxWidth - it's available on LazyItemScope .您可以使用Modifier.fillParentMaxWidth - 它在LazyItemScope上可用。 For example, to display 2 items use fraction = 0.5f :例如,要显示 2 个项目,请使用fraction = 0.5f

LazyRow(
    modifier = Modifier
        .fillMaxWidth()
        .height(200.dp)
) {
    item {
        FirstItem(
            modifier = Modifier
                .fillParentMaxWidth(0.5f)
        )
    }
    item {
        SecondItem(
            modifier = Modifier
                .fillParentMaxWidth(0.5f)
        )
    }
}

I'm pretty sure you don't want to use LazyRow .我很确定您不想使用LazyRow And for Row no problem.而对于Row没有问题。 Here's the code:这是代码:

Row(
    modifier = Modifier
        .fillMaxWidth()
        .height(200.dp)
) {
        FirstItem(
            modifier = Modifier.weight(1f)
        )
        SecondItem(
            modifier = Modifier.weight(1f)
        )
}

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

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