Box: ซ้อนและทับซ้อน
ซ้อนเนื้อหาพร้อมควบคุมการจัดแนว
Box: ซ้อนและทับซ้อน เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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.
คำถามที่พบบ่อย
บทเรียน “Box: ซ้อนและทับซ้อน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Box: ซ้อนและทับซ้อน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Box: ซ้อนและทับซ้อน”
ซ้อนเนื้อหาพร้อมควบคุมการจัดแนว คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “Box: ซ้อนและทับซ้อน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Column: เรียงสิ่งต่าง ๆ ในแนวตั้ง
- Row: วางสิ่งต่าง ๆ เคียงข้างกัน
- Box: ซ้อนและทับซ้อน
- การจัดเรียงและการจัดแนว