weight y tamaños flexibles
Distribuya proporcionalmente el espacio en filas y columnas.
weight y tamaños flexibles es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 4 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.
Flexible Space
Sometimes you want children to share space, not take fixed sizes. The weight modifier divides leftover room proportionally. ⚖️
Where weight Lives
weight works only inside a Row or Column. It is part of the layout scope those containers provide to their children.
Equal Split
Give two children the same weight and they share the space evenly, each taking half of what is available.
Box(Modifier.weight(1f))
Box(Modifier.weight(1f))Uneven Split
Weights are ratios. A child with weight(2f) gets twice the space of a sibling with weight(1f).
Box(Modifier.weight(2f))
Box(Modifier.weight(1f))Fixed Plus Flexible
Mix and match: give one child a fixed size and let another use weight to absorb whatever space remains.
Box(Modifier.width(80.dp))
Box(Modifier.weight(1f))Push to the Edge
A single weight on a spacer acts like a push, sending the next child all the way to the far end of the row.
Text("Left")
Spacer(Modifier.weight(1f))
Text("Right")Weights Run First
Compose measures fixed children first, then splits the leftover space among weighted ones. That is why weight needs siblings to push against.
The fill Flag
By default a weighted child fills its share. Pass fill = false to let it stay smaller than the space it was given.
Modifier.weight(1f, fill = false)Weighted Columns
The same idea works vertically. In a Column, weight divides the leftover height instead of width.
Column {
Box(Modifier.weight(1f))
Box(Modifier.weight(1f))
}Responsive by Design
Because weights are ratios, your layout scales gracefully across phone and tablet widths without hardcoded numbers.
A Common Pattern
Label on the left, value filling the rest: a weighted row is the classic recipe for clean settings and list rows.
Row {
Text("Name")
Text("Sam", Modifier.weight(1f))
}Quick Check
Two children sit in a Row with weight(3f) and weight(1f). How is the space shared?
Recap
You can now share space flexibly. weight splits leftover room by ratio inside Rows and Columns for responsive layouts. 🎯
Aprende Kotlin con un tutor de IA — gratis
Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.
- Cursos
- 30
- Lecciones
- 120
Preguntas frecuentes
¿La lección «weight y tamaños flexibles» es gratis?
Sí — el texto completo de «weight y tamaños flexibles» 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 «weight y tamaños flexibles»?
Distribuya proporcionalmente el espacio en filas y columnas. 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 4 de 4.
¿Cuánto tiempo toma la lección «weight y tamaños flexibles»?
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
- Padding, tamaño y fillMaxWidth
- Fondo, borde y clip
- Por qué importa el orden de los modificadores
- weight y tamaños flexibles