【问题标题】:Compose layout vertical alignment: SpaceBetween having no impact撰写布局垂直对齐:SpaceBetween 没有影响
【发布时间】:2021-09-10 05:18:40
【问题描述】:

我想将 Upcoming 文本推到底部,而 7 pax 文本仍然在顶部。我为该列使用了verticalArrangement = Arrangement.SpaceBetween,但它没有任何效果,因为该列没有占据整个高度。如果我对列使用fillMaxHeight 强制它占据全高,则整行占据全屏高度,这是意料之外的。我该如何解决这个问题?

@Composable
fun SoFixClientTourCard() {
    Column {
        Row(
            verticalAlignment = Alignment.Top,
            modifier = Modifier
                .background(Color.White)
                .fillMaxWidth()
                .padding(16.dp)
        ) {

            // calendar
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .padding(top = 4.dp)
                    .background(MaterialTheme.colors.error)
                    .shadow(1.dp)
                    .padding(horizontal = 8.dp, vertical = 4.dp)
            ) {
                Text("02", fontSize = 14.sp, fontWeight = FontWeight.Bold, color = Color.White)
                Text("OCT", fontSize = 12.sp, fontWeight = FontWeight.Bold, color = Color.White)
            }

            // tour & package name
            Column(
                modifier = Modifier
                    .padding(horizontal = 16.dp)
                    .weight(1f)
            ) {
                Text("Tanguar Haor Tour", style = MaterialTheme.typography.subtitle1, fontWeight = FontWeight.Bold)
                Text(
                    "Couple Non-AC + Single Non-AC + Family Non-AC + Couple AC",
                    style = MaterialTheme.typography.body2,
                    lineHeight = 20.sp,
                    modifier = Modifier.padding(top = 4.dp)
                )
            }

            // FIXME: Align status to the bottom, pax to the top
            // pax & status
            Column(
                horizontalAlignment = Alignment.End,
                verticalArrangement = Arrangement.SpaceBetween,
            ) {
                Text("7 pax", fontSize = XS)
                val status = "Upcoming"
                Text(
                    status,
                    fontSize = XXXS,
                    color = Color.White,
                    modifier = Modifier
                        .padding(top = 4.dp) // top margin
                        .background(
                            when (status.lowercase()) {
                                "upcoming" -> MaterialTheme.colors.primary
                                "cancelled" -> MaterialTheme.colors.error
                                else -> Color.DarkGray
                            }
                        )
                        .padding(horizontal = 8.dp, vertical = 4.dp)
                )
            }
        }
    }
}

感谢您的帮助!

【问题讨论】:

    标签: android kotlin android-jetpack-compose


    【解决方案1】:

    您可以将intrinsic measurements 应用于其父容器,在您的情况下为Row
    height(IntrinsicSize.Min) 修饰符添加到Row,并将fillMaxHeight() 修饰符添加到第三个Column。 比如:

    Column {
        Row(
            verticalAlignment = Alignment.Top,
            modifier = Modifier
                .background(Color.White)
                .fillMaxWidth()
                .padding(16.dp)
                .height(IntrinsicSize.Min)
        ) {
    
            //....
           
            // pax & status
            Column(
                modifier= Modifier.fillMaxHeight(),
                horizontalAlignment = Alignment.End,
                verticalArrangement = Arrangement.SpaceBetween,
            ) {
                //...
            }
        }
    }
    

    【讨论】:

    • 完美运行。非常感谢!
    • 嗨 @Gab 只是为了我的信息 pliz 评论 height(IntrinsicSize.Min)row 做了什么
    • @Tonnie Row 可组合的minIntrinsicHeight 将是其子级的最大minIntrinsicHeight。第三个Column 元素的minIntrinsicHeight0,因为如果没有给定约束,它不会占用空间;第一个和第二个 Column minIntrinsicHeight 将是给定特定宽度的内容。因此,Row 元素的高度约束将是第一个和第二个Column 的最大minIntrinsicHeight。然后第三个Column 将其高度扩展到Row 给出的高度约束。
    猜你喜欢
    • 2012-06-25
    • 2014-02-08
    • 2012-08-27
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多