【问题标题】:LazyColumn items draw on top of each otherLazyColumn 项目相互叠加
【发布时间】:2021-05-10 20:54:26
【问题描述】:

我使用 Jetpack Compose 版本 1.0.0-alpha01 和 LazyColumnFor 在列表中显示一些项目。更新到 1.0.0-beta06 后,我注意到 LazyColumnFor 已被弃用,取而代之的是 LazyColumn。然而,在迁移之后,列表中的项目会相互叠加,而不是垂直列出并在其间留出一些间距。

这里的奇怪之处在于,预览会按预期呈现项目,而安装在设备上的应用却没有。

这是活动:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val textStyle = TextStyle(color = Color.White, fontSize = 16.sp)
            val boldStyle = textStyle.merge(TextStyle(fontWeight = FontWeight.Bold))
            ComposeListTestTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    LazyColumn(modifier = Modifier.fillMaxHeight()) {
                        items(listOf("Row1", "Row2")) { day ->
                            Text(
                                text = day,
                                style = boldStyle,
                                modifier = Modifier.padding(top = 8.dp),
                                color = Color.Green
                            )
                        }
                    }
                }
            }
        }
    }
}

这是预览:

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    val textStyle = TextStyle(color = Color.White, fontSize = 16.sp)
    val boldStyle = textStyle.merge(TextStyle(fontWeight = FontWeight.Bold))
    ComposeListTestTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            LazyColumn(modifier = Modifier.fillMaxHeight()) {
                items(listOf("Row1", "Row2")) { day ->
                    Text(
                        text = day,
                        style = boldStyle,
                        modifier = Modifier.padding(top = 8.dp),
                        color = Color.Green
                    )
                }
            }
        }
    }
}

很遗憾,我无法让新的布局检查器在我的任何设备上工作,或者我怀疑我可能已经找到了一些线索来解释为什么会发生这种情况。

ComposeListTestTheme:

@Composable
fun ComposeListTestTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable() () -> Unit
) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }

    MaterialTheme(
        colors = colors,
        typography = Typography,
        shapes = Shapes,
        content = content
    )
}

【问题讨论】:

  • 请张贴屏幕
  • @GabrieleMariotti 我已经从预览和设备中添加了屏幕。
  • 我复制并粘贴了您的代码,它运行良好:/
  • 你的代码应该可以工作
  • 哦,感谢您的测试。我想知道我是否遇到了平台依赖错误(尽管我认为 Compose 是解耦的)。我在 Android Things(API 级别 28)设备上运行它,虽然我在运行 R 的 Pixel 2 上确实得到了相同的行为。如果我找到答案,我会进一步调试并回发..

标签: android kotlin android-jetpack-compose


【解决方案1】:

出现此问题是因为该应用未进行硬件加速。这已在 https://issuetracker.google.com/issues/187853238 中修复。

【讨论】:

  • 请注意,这在 API 30 和 API 31 上仍然存在问题
猜你喜欢
  • 2021-12-08
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 2019-04-10
  • 2023-03-31
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多