如何创建一个 Jetpack Compose Column,其中一个中间子项可滚动,但所有其他子项始终可见?

时间:2021-06-28 14:11:33

标签: android android-jetpack-compose

我正在创建一个 Jetpack Compose 对话框,其中包含一个列,其中所有元素都应该始终可见,除了第三个元素,如果文本不适合屏幕空间,该文本应该是可滚动的。我几乎通过为该 Text 元素创建的辅助可滚动列实现了这一点。但是,如果有很多文本,此实现会将底部子项(按钮)推出视图。这是我的代码:

@Composable
fun WelcomeView(
    viewModel: WelcomeViewModel,
    onDismiss: () -> Unit
) {
    Dialog(onDismissRequest = onDismiss) {
        Box(
            modifier = Modifier
                .clip(RoundedCornerShape(Spacing.extraLarge))
                .background(Colors.backgroundBase)
                .padding(all = Spacing.medium)
        ) {
            Column {
                IconView(
                    name = IconViewNames.RUNNING_SHOE,
                    size = IconViewSizes.LARGE,
                    color = Colors.primaryBase
                )

                Text(
                    viewModel.title,
                    style = Text.themeBillboard,
                    modifier = Modifier.padding(bottom = Spacing.medium)
                )

                Column(
                    modifier = Modifier
                        .verticalScroll(rememberScrollState())
                ) {
                    Text(
                        viewModel.message,
                        style = Text.themeHeadline,
                        modifier = Modifier.padding(bottom = Spacing.medium)
                    )
                }

                Button(
                    onClick = onDismiss,
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(Spacing.medium))
                        .background(Colors.primaryBase)
                ) {
                    Text(
                        "Continue",
                        style = Text.themeHeadline.copy(color = textExtraLight),
                        modifier = Modifier.padding(all = Spacing.extraSmall)
                    )
                }
            }
        }
    }
}

@Preview
@Composable
fun PreviewWelcomeView() {
    WelcomeView(
        viewModel = WelcomeViewModel(
            firstName = "Wendy",
            htmlWelcomeMessage = PreviewTextFixtures.threeParagraphs
        ),
        onDismiss = {}
    )
}

当只有一段文本时(正确)是这样的:

enter image description here

但这就是当有三个段落时的样子。文本滚动正确,但请注意缺少“继续”按钮:

enter image description here

2 个答案:

答案 0 :(得分:2)

只需将 Column 修饰符分配给可滚动的 weight
类似的东西:

    Column (verticalArrangement= Arrangement.SpaceBetween) {
            Text(
                "viewModel.title",
            )

            Column(
                modifier = Modifier
                    .verticalScroll(rememberScrollState())
                    .weight(1f, false)

            ) {
                Text("...")
            }

            Button() 
            
        }

答案 1 :(得分:2)

将此用于中间(可滚动)可组合

    Column(
        modifier = Modifier
            .verticalScroll(rememberScrollState())
            .weight(weight =1f, fill = false)

    ) {
        Text("Your text here")
    }

关键是使用fill = false