Jetpack Compose 中的约束布局:动态创建复杂且响应

2022-09-08  本文已影响0人  Darren老师

介绍

注意:本博客不会介绍约束布局的概念,但会介绍如何在 Jetpack Compose 中实现它。

先说基础!

start = 可组合的左侧

end = 可组合的右侧

image.png

我们真的每次都需要一个约束布局吗?

在使用 jetpack-compose 时,我们可能可以使用 Row/Column/Box 构建大部分 UI 内容,这使得事情变得非常简单,但我们可以构建一个很棒的 UI,如下所示:

但是,如果我们想构建一个复杂的布局,其中包括我们想要的可组合物的排列,如下所示?

ConstraintLayout 是一种布局,允许您相对于屏幕上的其他可组合项放置可组合项。它是使用多个嵌套行、列、框和自定义布局元素的替代方法。ConstraintLayout 在实现具有更复杂对齐要求的较大布局时很有用。

Jetpack Compose 中约束布局的实现

依赖项

implementation "androidx.constraintlayout:constraintlayout-compose:[version]"

执行

这是我们将用于在 jetpack compose 中实现约束布局的映射:

我们需要创建一个称为“ ConstraintSet{ } ”的东西,我们将在其中编写如何根据需要排列组合的代码。

val constraintsSet = ConstraintSet {
...
}

在 Jetpack-compose 中,我们需要通过createRefFor()为可组合对象创建一个引用,稍后我们可以使用修饰符将其添加到可组合对象中以访问其排列/对齐方式

val constraintsSet = ConstraintSet {
    val composable1= createRefFor("composable1")
    val composable2= createRefFor("composable2")
    val composable3= createRefFor("composable3")
}

为了根据我们的需要安排可组合项,我们需要通过constrain(variableName){ }附加可组合项的边界,并通过linkTo()将它们链接到相应的可组合项

val constraintsSet = ConstraintSet {  // constraint set
    val composable1= createRefFor("composable1")  // creating refernce
    val composable2= createRefFor("composable2")  // creating refernce
    val composable3= createRefFor("composable3")  // creating refernce

     constrain(composable1) {   // arranging "composable 1" bounds
                top.linkTo(parent.top)    // linking "composable1" top to "parent" top
                start.linkTo(parent.start)  // linking "composable1" start to "parent" start
                end.linkTo(parent.end)   // linking "composable1" end to "parent" end
     }

   constrain(composable2) {    // arranging "composable 2" bounds
                top.linkTo(composable1.bottom)    // linking "composable2" top to "composable1" bottom
                start.linkTo(composable1.start)  // linking "composable2" start to "composable1" start
     }

   constrain(composable3) {   // arranging "composable 3" bounds
                top.linkTo(composable1.bottom)    // linking "composable3" top to "composable1" bottom
                end.linkTo(composable1.end)  // linking "composable3" end to "composable1" end
     }

}

如果我们可视化上面的代码,我们的 UI 应该是这样的:

ConstraintLayout(
     constraintSet = constraintsSet,  // Don't forget to add "constraintsSet" variable
         modifier = Modifier  // The modifier assignment is upon you
                    .fillMaxSize()
    ) {
   // UI code
}

ConstraintLayout(
        constraintSet = constraintSet, modifier = Modifier
            .fillMaxSize()          
    ) {
        Box(     // composable1
            modifier = Modifier
                .padding(top=20.dp)
                .requiredWidth(250.dp)
                .requiredHeight(100.dp)
                .background(Color.White)
                .layoutId("composable1")   // reference 'id'
        )
        Box(     // composable2
            modifier = Modifier
                .padding(top=20.dp)
                .size(100.dp)
                .background(Color.LightGray)
                .layoutId("composable2")   // reference 'id'
        )
        Box(     // composable3
            modifier = Modifier
                .padding(top=20.dp)
                .size(100.dp)
                .background(Color.LightGray)
                .layoutId("composable3")   // reference 'id'
        )
    }

您的最终代码应与此类似

就这样结束了😉

撰写预览:

在移动设备上启动应用程序:

文章来源:https://saketh001.hashnode.dev/constraint-layout-in-jetpack-compose#heading-launching-the-application-on-a-mobile-device

上一篇 下一篇

猜你喜欢

热点阅读