简体   繁体   English

如何在 Jetpack Compose 中对齐底部一行?

[英]How align to bottom a row in Jetpack Compose?

I have a Column with some rows, and I want to align the last row at the botton, but this row is never located at the bottom of the screen, it stays right after the previous row:我有一个包含一些行的列,我想在底部对齐最后一行,但是这一行永远不会位于屏幕底部,它保持在前一行之后:

    Column {
        // RED BOX
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(130.dp)                
                .padding(vertical = 15.dp, horizontal = 30.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Column {
                Text(
                    text = stringResource(id = R.string.app_name),
                    style = TextStyle(fontSize = 40.sp),
                    color = Color.White
                )
                Text(
                    text = stringResource(id = R.string.app_description),
                    style = TextStyle(fontSize = 13.sp),
                    fontWeight = FontWeight.Bold,
                    color = Color.Black
                )
            }
        }

        Spacer(
            modifier = Modifier
                .fillMaxWidth()
                .height(15.dp)
        )

        // GREEN BOX
        val currentRoute = currentRoute(navController)
        items.forEach { item ->
            DrawerItem(item = item, selected = currentRoute == item.route) {
                navController.navigate(item.route) {
                    launchSingleTop = true
                }

                scope.launch {
                    scaffoldState.drawerState.close()
                }
            }
        }

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 15.dp, horizontal = 30.dp),
            verticalAlignment = Alignment.Bottom,
            horizontalArrangement = Arrangement.Center
        ) {
            Text(
                text = BuildConfig.VERSION_NAME,
                style = TextStyle(fontSize = 11.sp),
                color = Color.Black,
            )
        }
    }

I want to get the same as I show in the picture.我想得到和我在图片中显示的一样的东西。 I want to have the first row (red), then the second row (green) and then a third row that fits at the bottom of the screen (blue)我想要第一行(红色),然后是第二行(绿色),然后是适合屏幕底部的第三行(蓝色)

在此处输入图像描述

You can use a Spacer(modifier.weight(1f)) between GreenBox and Blue Box to create space between them or you can create your custom column with Layout function and set y position of last Placeable as height of Composable - height of last Composble您可以在 GreenBox 和 Blue Box 之间使用Spacer(modifier.weight(1f))在它们之间创建空间,或者您可以使用Layout function 创建自定义列,并将最后一个Placeable的 position 设置为Composable的高度 - 最后一个Composble的高度

    Column(modifier = Modifier
        .height(200.dp)
        .background(Color.LightGray)) {
        Text(
            "First Text",
            modifier = Modifier
                .background(Color(0xffF44336)),
            color = Color.White
        )
        Text(
            "Second Text",
            modifier = Modifier
                .background(Color(0xff9C27B0)),
            color = Color.White
        )
        Spacer(modifier = Modifier.weight(1f))
        Text(
            "Third Text",
            modifier = Modifier
                .background(Color(0xff2196F3)),
            color = Color.White
        )
    }

Result:结果:

在此处输入图像描述

Custom Layout自定义布局

@Composable
private fun CustomColumn(
    modifier: Modifier,
    content: @Composable () -> Unit
) {
    Layout(
        modifier = modifier,
        content = content
    ) { measurables, constraints ->

        val looseConstraints = constraints.copy(
            minWidth = 0,
            maxWidth = constraints.maxWidth,
            minHeight = 0,
            maxHeight = constraints.maxHeight
        )

        // Don't constrain child views further, measure them with given constraints
        // List of measured children
        val placeables = measurables.map { measurable ->
            // Measure each child
            measurable.measure(looseConstraints)
        }

        // Track the y co-ord we have placed children up to
        var yPosition = 0


        // Set the size of the layout as big as it can
        layout(constraints.maxWidth, constraints.maxHeight) {
            // Place children in the parent layout
            placeables.forEachIndexed { index, placeable ->

                println("Placeable width: ${placeable.width}, measuredWidth: ${placeable.measuredWidth}")
                // Position item on the screen
                if (index == placeables.size - 1) {
                    placeable.placeRelative(x = 0, y = constraints.maxHeight - placeable.height)
                } else {
                    placeable.placeRelative(x = 0, y = yPosition)
                }

                // Record the y co-ord placed up to
                yPosition += placeable.height
            }
        }
    }
}

Usage用法

    CustomColumn(
        modifier = Modifier
            .height(200.dp)
            .background(Color.LightGray)
    ) {
        Text(
            "First Text",
            modifier = Modifier
                .background(Color(0xffF44336)),
            color = Color.White
        )
        Text(
            "Second Text",
            modifier = Modifier
                .background(Color(0xff9C27B0)),
            color = Color.White
        )
        Spacer(modifier = Modifier.weight(1f))
        Text(
            "Third Text",
            modifier = Modifier
                .background(Color(0xff2196F3)),
            color = Color.White
        )
    }

Result:结果: 在此处输入图像描述

In this example with Layout you should consider how you measure your measureables with Constraints and your total width and height.在这个带有布局的示例中,您应该考虑如何使用约束和总宽度和高度来测量您的可测量值。 It requires a little bit practice but you get more unique designs and with less work(more optimised) composables than ready ones.它需要一点练习,但你会得到更多独特的设计,并且比现成的可组合的工作更少(更优化)。 Here i set layout as maxWidth so no matter which width you assign it takes whole width.在这里,我将布局设置为 maxWidth,因此无论您分配哪个宽度,它都需要整个宽度。 It's for demonstration you can set max width or height in layout based on your needs.它用于演示,您可以根据需要在layout中设置最大宽度或高度。

You can do it in many ways.您可以通过多种方式做到这一点。

You can use a Column with verticalArrangement = Arrangement.SpaceBetween assigning a weight(1f,false) to the last row:您可以使用带有verticalArrangement = Arrangement.SpaceBetween的 Column 为最后一行分配weight(1f,false)

  Column(
     Modifier.fillMaxHeight(),
     verticalArrangement = Arrangement.SpaceBetween) {
     
     //All elements 
     Column {

        // RED BOX
        
        //...
        Spacer(
            modifier = Modifier
                .fillMaxWidth()
                .background(Green)
                .height(15.dp)
        )

       //... Green box
    }

    //LAST ROW
    Row(
        modifier = Modifier
            .weight(1f, false)
    ) {
        //...
    }
 }

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在jetpack compose中将最后一行项目与底端对齐? - How to align the last row item to bottom end in jetpack compose? 如何在 Jetpack Compose 中按基线对齐行元素 - How to align row elements by the baseline in Jetpack Compose 在 Jetpack Compose 中对齐行项目 - Align row item in jetpack compose 如何在 Jetpack Compose 中将文本垂直对齐到顶部、底部和中心? - How to align Text to Top, Bottom and Center Vertically in Jetpack Compose? 将框/列与屏幕底部对齐 Jetpack Compose - Align Box/Column to bottom of screen Jetpack Compose 如何在 android jetpack compose Row 中自定义左/右对齐项目 - How to custom left/right align items in android jetpack compose Row Jetpack Compose 如何将具有特定大小的文本或文本组件的内容与左下角或右下角对齐? - Jetpack Compose How to Align text or content of Text component with specific size to bottom left or right? 如何居中对齐按钮文本(Jetpack Compose) - How to center align a button text (Jetpack Compose) 如何用jetpack compose把卡片放到底部? - how to place a card to the bottom with jetpack compose? 如何仅在 Jetpack Compose 的底部添加边框 - how to add border on bottom only in jetpack compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM