Box: sovrapporre e stratificare
Sovrapponga i contenuti controllandone l'allineamento.
Box: sovrapporre e stratificare è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Stacking on Top
When children should overlap rather than line up, you use a Box. It layers content on the same spot. 🗂️
Your First Box
A Box draws its children in order, each one on top of the previous. Later children sit above earlier ones, like stacked cards.
Box {
Image(painter, null)
Text("Caption")
}Z-Order Follows Source Order
The last child you write wins the top layer. So to put a label over a photo, place the Image first and the Text after it.
Box Sizes to Its Biggest Child
By default a Box wraps its largest child. The other children are positioned within that footprint instead of expanding it.
Aligning the Whole Box
The contentAlignment parameter sets a default spot for every child. Alignment.Center pulls them all to the middle of the Box.
Box(contentAlignment = Alignment.Center) {
Text("Centered")
}Aligning One Child
Inside a Box, each child can use the align modifier to override the default. This is how you pin one item to a corner.
Box {
Text("Top end", Modifier.align(Alignment.TopEnd))
}A Badge in the Corner
Overlap plus align gives you a classic badge: an avatar fills the Box while a small dot pins to the TopEnd corner.
Box {
Avatar()
Dot(Modifier.align(Alignment.TopEnd))
}Text Over an Image
A common pattern is a caption over a photo. Put the Image first, then align Text to BottomStart so it reads over the image.
Box {
Image(painter, null)
Text("Sunset", Modifier.align(Alignment.BottomStart))
}Filling and Backgrounds
Make a Box fill space with fillMaxSize, then add a background color. It becomes a handy layered container for a whole screen.
Box(Modifier.fillMaxSize().background(Color.Black)) {
Text("Hi")
}The Nine Alignment Spots
A Box offers nine alignment positions, from TopStart to BottomEnd, with Center in the middle. They cover every placement you need.
When to Reach for Box
Choose Box whenever pieces share the same area: overlays, badges, captions, or a spinner centered over content. It is your layering tool.
Quick Check
Let's confirm how a Box layers its children.
Recap: Box
You learned Box: it stacks children on the same spot in source order, sizes to its biggest child, and uses contentAlignment and align to place them.
Domande Frequenti
La lezione «Box: sovrapporre e stratificare» è gratuita?
Sì — il testo completo di «Box: sovrapporre e stratificare» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Cosa imparerò in «Box: sovrapporre e stratificare»?
Sovrapponga i contenuti controllandone l'allineamento. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Jetpack Compose Academy?
Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Box: sovrapporre e stratificare»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?
Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Column: impilare verticalmente gli elementi
- Row: disporre gli elementi affiancati
- Box: sovrapporre e stratificare
- Disposizione e allineamento