简体   繁体   English

如何让 Jetpack Compose 的 LazyGrid 在系统栏后面渲染?

[英]How to get Jetpack Compose's LazyGrid to render behind system bars?

I'm trying to use Accompanist's System UI Controller library along with WindowCompat.setDecorFitsSystemWindows(window, false) to render content behind a device's system UI.我正在尝试使用 Accompanist 的系统 UI 控制器库以及WindowCompat.setDecorFitsSystemWindows(window, false)在设备的系统 UI 后面呈现内容。

I'd like to also use Jetpack Compose's inset paddings to add some pad the top of its LazyVerticalGrid, however the grid doesn't render behind the system bars if I give it a padding with Modifier.statusBarsPadding() .我还想使用 Jetpack Compose 的插入填充在其 LazyVerticalGrid 的顶部添加一些填充,但是如果我使用Modifier.statusBarsPadding()给它一个填充,网格不会呈现在系统栏后面。

@Composable
fun libraryScreen(navController: NavController) {
  Surface(
    color = Color.Magenta,
    modifier = Modifier.fillMaxWidth()
  ) {
    Column(
      modifier = Modifier
        .fillMaxWidth()
        .padding(
          horizontal = 10.dp
        )
    ) {
      LazyVerticalGrid(
        columns = GridCells.Fixed(3),
        horizontalArrangement = Arrangement.spacedBy(10.dp),
        verticalArrangement = Arrangement.spacedBy(10.dp),
        modifier = Modifier
          .background(color = Color.Black)
//        .statusBarsPadding()
      ) {
        items(40) {
          albumCard("TITLE TITLE TITLE TITLE TITLE TITLE TITLE TITLE TITLE ", "ARTIST ARTIST ARTIST ARTIST ARTIST ARTIST ARTIST ")
        }
      }
    }

  }
}

Comparison GIFs:比较 GIF:

Without any padding没有任何填充

With padding带填充物

The same effect happens if I use a Box or Spacer above the grid.如果我在网格上方使用 Box 或 Spacer,也会出现同样的效果。

Accompanist insets are deprecated use WindowInsets library.不推荐使用伴奏插图使用WindowInsets库。

  contentPadding = WindowInsets.systemBars
                .only(WindowInsetsSides.Vertical)
                // Add if you want to have extra padding with your grid, optional
                .add(
                    WindowInsets(left = 8.dp, right = 8.dp, top = 16.dp, bottom = 16.dp)
                )
                .asPaddingValues(),

add will add extra padding initial padding and will scroll below status bar and navigation bar when you scroll your grid add 将添加额外的填充初始填充,并在您滚动网格时滚动到状态栏和导航栏下方

Surface(Modifier) {
    LazyVerticalGrid(
        contentPadding = WindowInsets.systemBars
            .only(WindowInsetsSides.Vertical)
            .add(
                WindowInsets(left = 8.dp, right = 8.dp, top = 16.dp, bottom = 16.dp)
            )
            .asPaddingValues(),
        modifier = Modifier
            .fillMaxSize()
            .background(backgroundColor),
        columns = GridCells.Fixed(3),
        content = {
            items(snacks) { snack: Snack ->
                GridSnackCard(snack = snack)
            }
        }
    )
}
                    

在此处输入图像描述

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

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