【问题标题】:Jetpack Compose Constraint Layout constrains not linkingJetpack Compose 约束布局约束不链接
【发布时间】:2021-03-19 04:53:27
【问题描述】:

我在 Jetpack Compose 中使用 constrainAs 将 wifi 选项列表限制在父视图的顶部,然后再到文本视图的底部。从照片中可以看出,我的列表并没有被限制在父级的顶部或它下面的文本视图,甚至被向上推离屏幕?

供参考,'list' 是 wifi 选项列表,'text1' 是以“Select your wifi”开头的文本视图

@Composable
fun ScanWifiScreen(wifiList: List<WifiOption>, onClick: (WifiOption) -> Unit) {
ConstraintLayout(
    modifier = Modifier
        .fillMaxSize()
        .background(colorResource(id = R.color.background))
) {

    val (list, text1, text2, progressIndicator) = createRefs()

    WifiList(
        wifiOptions = wifiList,
        onClick = onClick,
        modifier = Modifier
            .constrainAs(list) {
                top.linkTo(parent.top, margin = 8.dp)
                bottom.linkTo(text1.top)
                start.linkTo(parent.start)
                end.linkTo(parent.end)
            }
            .background(colorResource(id = R.color.background))
            .fillMaxHeight())

    Text(
        text = stringResource(id = R.string.select_wifi),
        modifier = Modifier
            .wrapContentSize()
            .padding(bottom = 16.dp)
            .constrainAs(text1) {
                bottom.linkTo(text2.top)
                start.linkTo(parent.start)
                end.linkTo(parent.end)
            },
        style = TextStyle(
            fontFamily = FontFamily(Font(R.font.quicksand_regular)),
            fontSize = 20.sp,
            color = colorResource(id = R.color.main_text),
            letterSpacing = 0.22.sp,
            textAlign = TextAlign.Center,
            lineHeight = 32.sp
        )
    )
    Text(
        text = stringResource(id = R.string.cant_see_network),
        modifier = Modifier
            .wrapContentSize()
            .padding(bottom = 32.dp)
            .constrainAs(text2) {
                bottom.linkTo(progressIndicator.top)
                start.linkTo(parent.start)
                end.linkTo(parent.end)
            },
        style = TextStyle(
            fontFamily = FontFamily(Font(R.font.quicksand_regular)),
            fontSize = 16.sp,
            color = colorResource(id = R.color.sub_text),
            letterSpacing = 0.18.sp,
            textAlign = TextAlign.Center,
            lineHeight = 24.sp
        )
    )

    ProgressIndicator2(
        progression = 3,
        modifier = Modifier.constrainAs(progressIndicator) {
            bottom.linkTo(parent.bottom)
            start.linkTo(parent.start)
            end.linkTo(parent.end)
        })
   }
}

【问题讨论】:

  • 添加top.linkTo(parent.top) 使text1 贴在屏幕顶部,添加top.linkTo(text1.bottom) 使list 低于text1
  • 这将使列表低于 text1,我需要高于 text1 的列表
  • 对于list -- top.linkTo(parent.top) 和对于text1 -- top.linkText(link.bottom)

标签: android android-jetpack android-jetpack-compose


【解决方案1】:

在您的列表中删除 .fillMaxHeight() 修饰符并添加约束 height = Dimension.fillToConstraints

 WifiList(
     //....
     modifier = Modifier
            .constrainAs(list) {
                top.linkTo(parent.top, margin = 8.dp)
                bottom.linkTo(text1.top)
                start.linkTo(parent.start)
                end.linkTo(parent.end)
                height = Dimension.fillToConstraints
            }
      )

【讨论】:

  • 完美运行!
  • 我认为 Dimension.fillToConstraints 应该是这个属性的默认值...因为是视图系统上 ConstraintLayout 的默认行为。
猜你喜欢
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 2022-11-02
  • 1970-01-01
  • 2019-03-29
相关资源
最近更新 更多