为什么 Android Studio 会抱怨这个

问题描述 投票:0回答:1

我正在关注此文档以了解如何在 Compose 中使用解耦 api ConstraintLayout ,但不幸的是未能构建和运行这是我得到的错误

Build Output

我不明白它为什么抱怨。我尝试使用光标来帮助我找到解决方案,但文档并没有这样做,并说我必须定义实际的字符串常量,这些常量将用作约束集中的引用,如下所示:

// Add these constants at the top of the file, outside any function
private const val imgProfile = "imgProfile"
private const val txtName = "txtName"
private const val txtCountry = "txtCountry"
private const val rowStats = "rowStats"
private const val btnFollow = "btnFollow"
private const val btnDM = "btnDM"

我的代码:


@Composable
fun ProfilePage(modifier: Modifier){
    Card(elevation = CardDefaults.cardElevation(defaultElevation = 6.dp),
        modifier = Modifier.fillMaxSize().padding(top = 220.dp, bottom = 220.dp, start = 16.dp, end = 16.dp)
    ) {
        BoxWithConstraints() {
            val constraints = if (minWidth < 600.dp) {
                verticalConstraints(margin = 16.dp)
            } else {
                verticalConstraints(margin = 16.dp)
            }

            ConstraintLayout(constraints) {
                Image(
                    painter = painterResource(id = R.drawable._84b21894c2453a6b598d23e62ff551c),
                    contentDescription = "Shizba",
                    modifier = Modifier
                        .size(100.dp)
                        .clip(CircleShape)
                        .border(width = 2.dp, shape = CircleShape, color = Color.Red)
                        .layoutId("imgProfile"),
                    contentScale = ContentScale.Crop
                )

                Text(
                    text = "Shiba Inu",
                    modifier = Modifier.layoutId("txtName")
                )
                Text(
                    "Japan",
                    modifier = Modifier.layoutId("txtCountry")
                )
                Row(
                    horizontalArrangement = Arrangement.SpaceEvenly,
                    modifier = Modifier
                        .fillMaxWidth() // it should take the entire width, so no need to link it to left or the right
                        .padding(16.dp)
                        .layoutId("rowStats")
                ) {
                    ProfileStats("350", "Followers")
                    ProfileStats("200", "Following")
                    ProfileStats("50", "Posts")
                }
                Button(
                    onClick = {},
                    modifier = Modifier.layoutId("btnFollow")
                ) {
                    Text(text = "Follow User")
                }
                Button(
                    onClick = {},
                    modifier = Modifier.layoutId("btnDM")
                ) {
                    Text(text = "Send Message")
                }
            }
        }
    }
}

@Composable
fun ProfileStats(count: String, title: String){
    Column(horizontalAlignment = Alignment.CenterHorizontally)
    {
        Text(text = count, fontWeight = FontWeight.Bold)
        Text(text = title)
    }
}


private fun verticalConstraints(margin:Dp): ConstraintSet{
    return ConstraintSet{
        val guideline = createGuidelineFromTop(0.1f)
        val imgProfile = createRefFor(imgProfile)
        val txtName = createRefFor(txtName)
        val txtCountry = createRefFor(txtCountry)
        val rowStats = createRefFor(rowStats)
        val btnFollow = createRefFor(btnFollow)
        val btnDM = createRefFor(btnDM)

        constrain(imgProfile){
            top.linkTo(parent.top)
            start.linkTo(parent.start)
            end.linkTo(parent.end)
        }
        constrain(txtName){
            top.linkTo(imgProfile.bottom)
            start.linkTo(parent.start)
            end.linkTo(parent.end)
        }
        constrain(txtCountry){
            top.linkTo(txtName.bottom)
            start.linkTo(parent.start)
            end.linkTo(parent.end)
        }
        constrain(rowStats){
            top.linkTo(txtCountry.bottom)
        }
        constrain(btnFollow){
            top.linkTo(rowStats.bottom, margin = margin)
            start.linkTo(parent.start)
            end.linkTo(btnDM.start)
            width = Dimension.wrapContent
        }
        constrain(btnDM){
            top.linkTo(rowStats.bottom, margin = margin)
            start.linkTo(btnFollow.end)
            end.linkTo(parent.end)
            width = Dimension.wrapContent
        }
    }

}
android kotlin android-studio android-jetpack-compose android-constraintlayout
1个回答
0
投票

您应该为字符串创建引用,如下所示:

val imgProfile = createRefFor("imgProfile")
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.