I have this usecase that I thought will be easy to...
# compose
p
I have this usecase that I thought will be easy to achieve, but I stuck. There is a Row with 3 elements of different width. I want to place them in the same distance, but distance should be measured from the center of the object, not the gap between them. Here is the screenshot of standard Row with Arrangement.SpaceBetween. How can I position them , so the x1 == x2 ?
o
You can write your own
Arrangement
that would do what you need. It’s not hard. This looks like
justified
arrangement I wrote recently. It’s not perfect for RTL/LTR and maybe something else, but it could give you an idea
Copy code
fun Arrangement.justified(minSpace: Dp): Arrangement.HorizontalOrVertical = object : Arrangement.HorizontalOrVertical {
    override val spacing: Dp
        get() = minSpace

    override fun Density.arrange(totalSize: Int, sizes: IntArray, layoutDirection: LayoutDirection, outPositions: IntArray) {
        if (sizes.isEmpty()) return
        val minSpacePx = minSpace.roundToPx()
        val measuredSize = sizes.sum()
        val spacePx = if (sizes.size > 1)
            kotlin.math.max((totalSize - measuredSize) / (sizes.size - 1), minSpacePx)
        else
            minSpacePx

        var occupied = 0
        var lastSpace = 0
        val reversed = layoutDirection == LayoutDirection.Rtl
        sizes.forEachIndexed(reversed) { index, it ->
            outPositions[index] = kotlin.math.min(occupied, totalSize - it)
            lastSpace = kotlin.math.min(spacePx, totalSize - outPositions[index] - it)
            occupied = outPositions[index] + it + lastSpace
        }
    }

    override fun Density.arrange(totalSize: Int, sizes: IntArray, outPositions: IntArray) =
        arrange(totalSize, sizes, LayoutDirection.Ltr, outPositions)

    override fun toString() = "Arrangement#justified($minSpace)"
}
d
It’s not hard
Displays hard math. (Math, also known as an esoteric language embedded in the fabric of reality by what most men would call aliens.)
“Hard” is a matter of perspective and circumstance.