[英]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
)
}
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.