Google is committed to advancing racial equity for Black communities. See how.

androidx.compose.ui.layout

Interfaces

ContentScale

Represents a rule to apply to scale a source rectangle to be inscribed into a destination

IntrinsicMeasurable

A part of the composition that can be measured.

IntrinsicMeasureScope

The receiver scope of a layout's intrinsic measurements lambdas.

LayoutCoordinates

A holder of the measured bounds for the layout (MeasureBox).

LayoutIdParentData

Can be implemented by values used as parent data to make them usable as tags.

LayoutModifier

A Modifier.Element that changes how its wrapped content is measured and laid out.

Measurable

A part of the composition that can be measured.

MeasureResult

Interface holding the size and alignment lines of the measured layout, as well as the children positioning logic.

MeasureScope

The receiver scope of a layout's measure lambda.

OnGloballyPositionedModifier

A modifier whose onGloballyPositioned is called with the final LayoutCoordinates of the Layout when the global position of the content may have changed.

OnPositionedModifier

OnRemeasuredModifier

A modifier whose onRemeasured is called when the layout content is remeasured.

ParentDataModifier

A Modifier that provides data to the parent Layout.

Remeasurement

This object is associated with a layout node and allows to execute some extra measure/layout actions which are needed for some complex layouts.

RemeasurementModifier

A Modifier.Element that provides a Remeasurement object associated with the layout node the modifier is applied to.

SubcomposeMeasureScope

The receiver scope of a SubcomposeLayout's measure lambda which adds ability to dynamically subcompose a content during the measuring on top of the features provided by MeasureScope.

WithConstraintsScope

Receiver scope being used by the children parameter of WithConstraints

Classes

AlignmentLine

Defines an offset line that can be used by parent layouts to align and position their children.

FixedScale

ContentScale implementation that always scales the dimension by the provided fixed floating point value

HorizontalAlignmentLine

A horizontal AlignmentLine.

Measured

Read-only wrapper over Placeable that exposes the measurement result with no placing ability.

Placeable

A Placeable corresponds to a child layout that can be positioned by its parent layout.

ScaleFactor

Holds 2 dimensional scaling factors for horizontal and vertical axes

VerticalAlignmentLine

A vertical AlignmentLine.

Type-aliases

IntrinsicMeasureBlock

A function for performing intrinsic measurement.

MeasureBlock

A function for performing layout measurement.

Top-level functions summary

Unit
Layout(children: () -> Unit, minIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock, minIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock, maxIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock, maxIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock, modifier: Modifier = Modifier, measureBlock: MeasureBlock)

Layout is the main core component for layout.

Unit
Layout(children: () -> Unit, modifier: Modifier = Modifier, measureBlock: MeasureBlock)

Layout is the main core component for layout.

Unit
Layout(children: () -> Unit, measureBlocks: LayoutNode.MeasureBlocks, modifier: Modifier = Modifier)

Layout is the main core component for layout.

LayoutNode.MeasureBlocks

Default LayoutNode.MeasureBlocks object implementation, providing intrinsic measurements that use the measure block replacing the measure calls with intrinsic measurement calls.

Unit
MultiMeasureLayout(modifier: Modifier = Modifier, children: () -> Unit, measureBlock: MeasureBlock)

ScaleFactor
ScaleFactor(scaleX: Float, scaleY: Float)

Constructs a ScaleFactor from the given x and y scale values

Unit
SubcomposeLayout(modifier: Modifier = Modifier, measureBlock: SubcomposeMeasureScope<T>.(Constraints) -> MeasureResult)

Analogue of Layout which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children.

Unit
WithConstraints(modifier: Modifier = Modifier, children: WithConstraintsScope.() -> Unit)

A composable that defines its own content according to the available space, based on the incoming constraints or the current LayoutDirection.

ScaleFactor
lerp(start: ScaleFactor, stop: ScaleFactor, fraction: Float)

Linearly interpolate between two ScaleFactor parameters

LayoutNode.MeasureBlocks
measureBlocksOf(minIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock, minIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock, maxIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock, maxIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock, measureBlock: MeasureBlock)

Creates an instance of LayoutNode.MeasureBlocks to pass to Layout given intrinsic measures and a measure block.

Top-level properties summary

HorizontalAlignmentLine

AlignmentLine defined by the baseline of a first line of a androidx.foundation.text.CoreText

HorizontalAlignmentLine

AlignmentLine defined by the baseline of the last line of a androidx.foundation.text.CoreText

Extension functions summary

For Size
operator Size
Size.div(scaleFactor: ScaleFactor)

Division operator with Size

operator Size
Size.times(scaleFactor: ScaleFactor)

Multiplication operator with Size.

For Modifier
Modifier

Creates a LayoutModifier that allows changing how the wrapped element is measured and laid out.

Modifier

Tag the element with id to identify the element within its parent.

Modifier
Modifier.onGloballyPositioned(crossinline onGloballyPositioned: (LayoutCoordinates) -> Unit)

Invoke onGloballyPositioned with the LayoutCoordinates of the element when the global position of the content may have changed.

Modifier

Modifier
Modifier.onSizeChanged(crossinline onSizeChanged: (IntSize) -> Unit)

Invoke onSizeChanged with the size of the content after it has been measured.

For ScaleFactor
operator Size

Multiplication operator with Size with reverse parameter types to maintain commutative properties of multiplication

Extension properties summary

For LayoutCoordinates
Rect

Returns the bounding box of the child in the parent's content area, including any clipping done with respect to the parent.

Rect

The boundaries of this layout inside the root composable.

Rect

The global boundaries of this layout inside.

Offset

The global position of this layout.

Offset

Returns the position of the top-left in the parent's content area or (0, 0) for the root.

Offset

The position of this layout inside the root composable.

For Measurable
Any?

Retrieves the tag associated to a composable with the Modifier.layoutId modifier.

Top-level functions

Layout

@Composable fun Layout(
    children: () -> Unit,
    minIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock,
    minIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock,
    maxIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock,
    maxIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock,
    modifier: Modifier = Modifier,
    measureBlock: MeasureBlock
): Unit

Layout is the main core component for layout. It can be used to measure and position zero or more children.

Intrinsic measurement blocks define the intrinsic sizes of the current layout. These can be queried by the parent in order to understand, in specific cases, what constraints should the layout be measured with:

  • minIntrinsicWidthMeasureBlock defines the minimum width this layout can take, given a specific height, such that the content of the layout will be painted correctly
  • minIntrinsicHeightMeasureBlock defines the minimum height this layout can take, given a specific width, such that the content of the layout will be painted correctly
  • maxIntrinsicWidthMeasureBlock defines the minimum width such that increasing it further will not decrease the minimum intrinsic height
  • maxIntrinsicHeightMeasureBlock defines the minimum height such that increasing it further will not decrease the minimum intrinsic width

For a composable able to define its content according to the incoming constraints, see WithConstraints.

Example usage:

import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.Constraints

// We build a layout that will occupy twice as much space as its children,
// and will position them to be bottom right aligned.
Layout(
    children,
    minIntrinsicWidthMeasureBlock = { measurables, h ->
        // The min intrinsic width of this layout will be twice the largest min intrinsic
        // width of a child. Note that we call minIntrinsicWidth with h / 2 for children,
        // since we should be double the size of the children.
        (measurables.map { it.minIntrinsicWidth(h / 2) }.maxByOrNull { it } ?: 0) * 2
    },
    minIntrinsicHeightMeasureBlock = { measurables, w ->
        (measurables.map { it.minIntrinsicHeight(w / 2) }.maxByOrNull { it } ?: 0) * 2
    },
    maxIntrinsicWidthMeasureBlock = { measurables, h ->
        (measurables.map { it.maxIntrinsicHeight(h / 2) }.maxByOrNull { it } ?: 0) * 2
    },
    maxIntrinsicHeightMeasureBlock = { measurables, w ->
        (measurables.map { it.maxIntrinsicHeight(w / 2) }.maxByOrNull { it } ?: 0) * 2
    }
) { measurables, constraints ->
    // measurables contains one element corresponding to each of our layout children.
    // constraints are the constraints that our parent is currently measuring us with.
    val childConstraints = Constraints(
        minWidth = constraints.minWidth / 2,
        minHeight = constraints.minHeight / 2,
        maxWidth = constraints.maxWidth / 2,
        maxHeight = constraints.maxHeight / 2
    )
    // We measure the children with half our constraints, to ensure we can be double
    // the size of the children.
    val placeables = measurables.map { it.measure(childConstraints) }
    val layoutWidth = (placeables.maxByOrNull { it.width }?.width ?: 0) * 2
    val layoutHeight = (placeables.maxByOrNull { it.height }?.height ?: 0) * 2
    // We call layout to set the size of the current layout and to provide the positioning
    // of the children. The children are placed relative to the current layout place.
    layout(layoutWidth, layoutHeight) {
        placeables.forEach {
            it.placeRelative(layoutWidth - it.width, layoutHeight - it.height)
        }
    }
}
Parameters
children: () -> Unit The children composable to be laid out.
modifier: Modifier = Modifier Modifiers to be applied to the layout.
minIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock The minimum intrinsic width of the layout.
minIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock The minimum intrinsic height of the layout.
maxIntrinsicWidthMeasureBlock: IntrinsicMeasureBlock The maximum intrinsic width of the layout.
maxIntrinsicHeightMeasureBlock: IntrinsicMeasureBlock The maximum intrinsic height of the layout.
measureBlock: MeasureBlock The block defining the measurement and positioning of the layout.

Layout

@Composable fun Layout(
    children: () -> Unit,
    modifier: Modifier = Modifier,
    measureBlock: MeasureBlock
): Unit

Layout is the main core component for layout. It can be used to measure and position zero or more children.

The intrinsic measurements of this layout will be calculated by running the measureBlock, while swapping measure calls with appropriate intrinsic measurements. Note that these provided implementations will not be accurate in all cases - when this happens, the other overload of Layout should be used to provide correct measurements.

For a composable able to define its content according to the incoming constraints, see WithConstraints.

Example usage:

import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.Constraints

// We build a layout that will occupy twice as much space as its children,
// and will position them to be bottom right aligned.
Layout(children) { measurables, constraints ->
    // measurables contains one element corresponding to each of our layout children.
    // constraints are the constraints that our parent is currently measuring us with.
    val childConstraints = Constraints(
        minWidth = constraints.minWidth / 2,
        minHeight = constraints.minHeight / 2,
        maxWidth = constraints.maxWidth / 2,
        maxHeight = constraints.maxHeight / 2
    )
    // We measure the children with half our constraints, to ensure we can be double
    // the size of the children.
    val placeables = measurables.map { it.measure(childConstraints) }
    val layoutWidth = (placeables.maxByOrNull { it.width }?.width ?: 0) * 2
    val layoutHeight = (placeables.maxByOrNull { it.height }?.height ?: 0) * 2
    // We call layout to set the size of the current layout and to provide the positioning
    // of the children. The children are placed relative to the current layout place.
    layout(layoutWidth, layoutHeight) {
        placeables.forEach {
            it.placeRelative(layoutWidth - it.width, layoutHeight - it.height)
        }
    }
}
Parameters
children: () -> Unit The children composable to be laid out.
modifier: Modifier = Modifier Modifiers to be applied to the layout.
measureBlock: MeasureBlock The block defining the measurement and positioning of the layout.

See Also