Compose :由于“WindowCompat.setDecorFitsSystemWindows(window, false)”而搭建不必要的 systemBar 填充

X-B*_*... 8 android material-components-android android-jetpack-compose android-jetpack-compose-material3

在使用edge2edge显示应用程序时,我遇到了一种情况scaffold,根据官方文档,脚手架填充值用于偏移顶部和底部栏(如果存在)。事实证明,当我使用脚手架而不提供顶部/底部参数时,脚手架的内容会自动在顶部和底部填充空白

topBar如果我为或提供参数bottomBar,则另一个参数会自动填充空格。

Catch :WindowCompat.setDecorFitsSystemWindows(window, false)这仅在使用时发生

代码片段:

WindowCompat.setDecorFitsSystemWindows(window, false) //commenting this line of code gives desired results but defeates the E2E display

    setContent {
        TestTheme {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.Cyan)
            ) {
                Scaffold(
                    modifier = Modifier
                        .fillMaxSize(0.8f)
                        .align(Alignment.Center),

                    ) { paddingValues ->

                    Box(
                        modifier = Modifier
                            .padding(paddingValues)
                            .fillMaxSize()
                            .background(Color.White)
                    ) {

                        Log.d("scaffold", "padding values = $paddingValues")

                        Text("Android")
                    }

                }
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

未评论: ------ 评论:

未发表评论----- 评论了

问:请问,这种行为的原因是什么?如何在不添加不必要的填充的情况下使用脚手架实现有效的 E2E?

开发工具:

最新Android Studio(电鳗)

androidx.compose.material3

Mer*_*ost 6

有两种选择:

  1. 不要使用 Scaffold 的 paddingValues

  1. 将零 contentWindowInsets 设置为 Scaffold,如下所示:

     Scaffold(
         contentWindowInsets = WindowInsets(0.dp), 
         ...
     ) {
         //Your content
     }
    
    Run Code Online (Sandbox Code Playgroud)

默认值为ScaffoldDefaults.contentWindowInsets,由以下部分组成WindowInsets.systemBars

Google 的开发人员也使用这种方法