Colton Idle
03/09/2021, 4:20 PMRed
composable and Blue
composable that I want to overlap by 35%. The three ways I've thought about doing this is:
1. Have these two in a Box
, but apply a padding to the top of blue composable that I calculate somehow to be 35% of bottom of red
2. Have these two in a Column, and someone apply a negative padding to a value that I calculate somehow to be 35% of the bottom of red
3. Write a custom layout? (seems scary even though this is supposed to be a lot easier in compose)
Anyone have other ideas or an idea of how I would go about this? Right now I just hard coded the padding and it works, but the height of the red square will change and so the hard coded padding idea will fall apart.
Edit: Wish there was a way to shrink the preview size in slack. SorryLilly
03/09/2021, 5:00 PMBradleycorn
03/09/2021, 6:40 PMColumn
to have it's content aligned 35% down from the top of the screen, and @Adam Powell pointed me in the right direction to write a custom Arrangement
to achieve it. It turned out to be really easy, and you can see the code in the post. In the end, you write something like this:
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.fromTop(0.35F)) {
...
}
Bradleycorn
03/09/2021, 6:41 PMfromTop
arrangement:
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1615040563494000Bradleycorn
03/09/2021, 6:49 PMArrangement.overlapBy(percent: Float)
method pretty easilyAdam Powell
03/09/2021, 7:04 PMBradleycorn
03/09/2021, 7:13 PMfun Arrangement.overlapBy(@FloatRange(from = 0.0, to = 1.0) percent: Float): Arrangement.Vertical {
return object: Arrangement.HorizontalOrVertical {
override val spacing = 0.dp
override fun Density.arrange(totalSize: Int, sizes: IntArray, outPositions: IntArray) {
placeItems(percent, totalSize, sizes, outPositions)
}
override fun Density.arrange(
totalSize: Int,
sizes: IntArray,
layoutDirection: LayoutDirection,
outPositions: IntArray
) {
when (layoutDirection) {
LayoutDirection.Ltr -> placeItems(percent, totalSize, sizes, outPositions)
else -> {
sizes.reverse()
placeItems(percent, totalSize, sizes, outPositions)
outPositions.reverse()
}
}
}
private fun placeItems(percent: Float, totalSize: Int, sizes: IntArray, outPositions: IntArray) {
sizes.foldIndexed(0) { index, acc, size ->
outPositions[index] = acc
acc + (size * (1 - percent)).roundToInt()
}
}
}
}
Bradleycorn
03/09/2021, 7:13 PMColumn(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.overlapBy(.35F)) {
Surface(color = Color(0xFFFF0000), modifier = Modifier.fillMaxWidth().requiredHeight(100.dp)) {}
Surface(color = Color(0xFF0000FF), modifier = Modifier.fillMaxWidth().requiredHeight(100.dp)) {}
Surface(color = Color(0xFF00FF00), modifier = Modifier.fillMaxWidth().requiredHeight(100.dp)) {}
}
Bradleycorn
03/09/2021, 7:14 PMBradleycorn
03/09/2021, 7:15 PMColton Idle
03/09/2021, 7:42 PMiirc Arrangement doesn't affect the measurement of the items or final container size, so that may not work hereAs Adam says the original size of the view is still actually the same size.
Bradleycorn
03/09/2021, 7:48 PMColton Idle
03/09/2021, 7:54 PMColton Idle
03/22/2021, 12:24 AM.matchParentSize()
.wrapContentHeight(<http://Alignment.Top|Alignment.Top>)
.fillMaxHeight(0.5f)
😄
I'm going to hack on this a little tonight, to see if I can save myself from a custom layout.Adam Powell
03/22/2021, 8:13 PMAdam Powell
03/22/2021, 8:14 PMAdam Powell
03/22/2021, 8:16 PMColton Idle
03/22/2021, 8:26 PM