Escribir un Composable Layout
Mida los elementos secundarios y colóquelos manualmente.
Escribir un Composable Layout es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Jetpack Compose Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Meet the Layout Composable
To build your own arrangement, you reach for the Layout composable. It hands you the raw measure-and-place power directly. 🛠️
Layout Takes Content and a Lambda
Layout accepts your child content plus a measure lambda. The lambda receives the measurables and the constraints to work with.
Layout(content = content,
modifier = modifier) { measurables, constraints ->
// measure and place
}Step One: Measure the Children
Inside the lambda, map over the measurables and measure each one. You get a list of placeables with known sizes back.
val placeables = measurables.map { it.measure(constraints) }Step Two: Decide Your Own Size
Now compute how big your layout should be. For a vertical stack, the height is the sum of child heights and the width is the widest child.
val height = placeables.sumOf { it.height }
val width = placeables.maxOf { it.width }Step Three: Report Size with layout()
Call layout(width, height) to declare your size. The trailing lambda is where you actually position every child.
layout(width, height) {
// placement goes here
}Step Four: Place Each Child
Track a running y offset and place each placeable beneath the last. Add its height to the offset after each placeRelative call.
var y = 0
placeables.forEach {
it.placeRelative(0, y)
y += it.height
}You Just Rebuilt Column
Stacking children top to bottom is exactly what Column does. You have now written a layout from scratch that behaves just like it. 🎉
Wrap It in a Reusable Function
Put your Layout call inside a @Composable function so others can use it like any built-in. Expose a modifier and content slot.
@Composable
fun MyColumn(modifier: Modifier = Modifier,
content: @Composable () -> Unit) { }Pass the Modifier Through
Forward the incoming modifier to the Layout call. This lets callers add padding, size, and background just like with real layouts.
Default the Modifier Parameter
Give modifier a default of Modifier. Callers who do not need extra styling can skip it, keeping your API clean and friendly.
Now You Can Customize Anything
Change the placement math and you get diagonal stacks, centered rows, or grids. The Layout composable is your blank canvas for arrangement.
Quick Check
Which method actually positions a measured child on screen?
Recap: Writing a Layout Composable
Inside Layout you measure children to placeables, call layout() to set your size, then placeRelative each child. Wrap it in a function for reuse. ✅
Preguntas frecuentes
¿La lección «Escribir un Composable Layout» es gratis?
Sí — el texto completo de «Escribir un Composable Layout» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Jetpack Compose Academy, actualiza a CoddyKit PRO. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Escribir un Composable Layout»?
Mida los elementos secundarios y colóquelos manualmente. Practicas Jetpack Compose Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Jetpack Compose Academy?
No se requiere experiencia previa. Jetpack Compose Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Escribir un Composable Layout»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Jetpack Compose Academy?
Sí. Cada lección de Jetpack Compose Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- El modelo de medir y colocar
- Escribir un Composable Layout
- Restricciones y medidas intrínsecas
- Un FlowRow que ajusta los chips