LayoutModifier


A Modifier.Element that changes how its wrapped content is measured and laid out. It has the same measurement and layout functionality as the androidx.compose.ui.layout.Layout component, while wrapping exactly one layout due to it being a modifier. In contrast, the androidx.compose.ui.layout.Layout component is used to define the layout behavior of multiple children.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.offset

val verticalPadding = object : LayoutModifier {
    override fun MeasureScope.measure(
        measurable: Measurable,
        constraints: Constraints
    ): MeasureResult {
        // an example modifier that adds 50 pixels of vertical padding.
        val padding = 50
        val placeable = measurable.measure(constraints.offset(vertical = -padding))
        return layout(placeable.width, placeable.height + padding) {
            placeable.placeRelative(0, padding)
        }
    }
}
Box(Modifier.background(Color.Gray).then(verticalPadding)) {
    Box(Modifier.fillMaxSize().background(Color.DarkGray))
}
See also
Layout

Summary

Public functions

open Int
IntrinsicMeasureScope.maxIntrinsicHeight(
    measurable: IntrinsicMeasurable,
    width: Int
)

The lambda used to calculate IntrinsicMeasurable.maxIntrinsicHeight.

Cmn
open Int
IntrinsicMeasureScope.maxIntrinsicWidth(
    measurable: IntrinsicMeasurable,
    height: Int
)

The function used to calculate IntrinsicMeasurable.maxIntrinsicWidth.

Cmn
MeasureResult
MeasureScope.measure(measurable: Measurable, constraints: Constraints)

The function used to measure the modifier.

Cmn
open Int
IntrinsicMeasureScope.minIntrinsicHeight(
    measurable: IntrinsicMeasurable,
    width: Int
)

The lambda used to calculate IntrinsicMeasurable.minIntrinsicHeight.

Cmn
open Int
IntrinsicMeasureScope.minIntrinsicWidth(
    measurable: IntrinsicMeasurable,
    height: Int
)

The function used to calculate IntrinsicMeasurable.minIntrinsicWidth.

Cmn

Inherited functions

From androidx.compose.ui.Modifier
open infix Modifier
then(other: Modifier)

Concatenates this modifier with another.

Cmn
From androidx.compose.ui.Modifier.Element
open Boolean
all(predicate: (Modifier.Element) -> Boolean)

Returns true if predicate returns true for all Elements in this Modifier or if this Modifier contains no Elements.

Cmn
open Boolean
any(predicate: (Modifier.Element) -> Boolean)

Returns true if predicate returns true for any Element in this Modifier.

Cmn
open R
<R : Any?> foldIn(initial: R, operation: (Modifier.Element, R) -> R)

Accumulates a value starting with initial and applying operation to the current value and each element from outside in.

Cmn
open R
<R : Any?> foldOut(initial: R, operation: (Modifier.Element, R) -> R)

Accumulates a value starting with initial and applying operation to the current value and each element from inside out.

Cmn

Public functions

maxIntrinsicHeight

open fun IntrinsicMeasureScope.maxIntrinsicHeight(
    measurable: IntrinsicMeasurable,
    width: Int
): Int

The lambda used to calculate IntrinsicMeasurable.maxIntrinsicHeight.

maxIntrinsicWidth

open fun IntrinsicMeasureScope.maxIntrinsicWidth(
    measurable: IntrinsicMeasurable,
    height: Int
): Int

The function used to calculate IntrinsicMeasurable.maxIntrinsicWidth.

measure

fun MeasureScope.measure(measurable: Measurable, constraints: Constraints): MeasureResult

The function used to measure the modifier. The measurable corresponds to the wrapped content, and it can be measured with the desired constraints according to the logic of the LayoutModifier. The modifier needs to choose its own size, which can depend on the size chosen by the wrapped content (the obtained Placeable), if the wrapped content was measured. The size needs to be returned as part of a MeasureResult, alongside the placement logic of the Placeable, which defines how the wrapped content should be positioned inside the LayoutModifier. A convenient way to create the MeasureResult is to use the MeasureScope.layout factory function.

A LayoutModifier uses the same measurement and layout concepts and principles as a Layout, the only difference is that they apply to exactly one child. For a more detailed explanation of measurement and layout, see MeasurePolicy.

minIntrinsicHeight

open fun IntrinsicMeasureScope.minIntrinsicHeight(
    measurable: IntrinsicMeasurable,
    width: Int
): Int

The lambda used to calculate IntrinsicMeasurable.minIntrinsicHeight.

minIntrinsicWidth

open fun IntrinsicMeasureScope.minIntrinsicWidth(
    measurable: IntrinsicMeasurable,
    height: Int
): Int

The function used to calculate IntrinsicMeasurable.minIntrinsicWidth.