โมเดลการวัดและการวางตำแหน่ง
ทำความเข้าใจเลย์เอาต์แบบผ่านครั้งเดียวของ Compose
โมเดลการวัดและการวางตำแหน่ง เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
When Built-In Layouts Are Not Enough
Column, Row, and Box cover most screens. But sometimes you need total control over where children go. That is when you build a custom layout. 🎨
Compose Lays Out in One Pass
Unlike the old View system, Compose measures and places everything in a single pass. There is no expensive measure-then-measure-again cycle.
Two Steps: Measure, Then Place
Every layout does two things in order. First it measures each child to learn its size. Then it places each child at a chosen position.
Measurables Are Children Not Yet Sized
A parent receives its children as measurables. A measurable is a child you have not measured yet, like a parcel before you weigh it. 📦
Measuring Produces a Placeable
Call measure on a measurable and you get a placeable back. The placeable knows its final width and height and is ready to be positioned.
val placeable = measurable.measure(constraints)
val w = placeable.widthConstraints Set the Size Limits
When you measure, you pass constraints: the min and max width and height the child is allowed. The child picks a size within those bounds.
Measure Each Child Exactly Once
The single-pass rule means you must measure each child only once. Measuring the same child twice throws an error at runtime.
The Parent Decides Its Own Size
After measuring children, the parent reports its own width and height. It usually sizes itself to wrap the children it just measured.
layout(width, height) {
// place children here
}Placing Sets Each Child's Position
Inside the layout block you call placeRelative on each placeable, giving it an x and y. That is where the child lands on screen.
placeable.placeRelative(x = 0, y = 0)placeRelative Respects Direction
Prefer placeRelative over place. It automatically mirrors positions for right-to-left languages, so your layout works everywhere. 🌍
This Model Powers Every Layout
Column, Row, and your future custom layouts all follow this same measure-then-place rhythm. Learn it once and you understand all of Compose layout.
Quick Check
What do you get back after measuring a measurable?
Recap: The Measure & Place Model
Compose lays out in one pass: measure each child once within constraints to get placeables, then place them. This model drives every layout you will build. ✅
คำถามที่พบบ่อย
บทเรียน “โมเดลการวัดและการวางตำแหน่ง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โมเดลการวัดและการวางตำแหน่ง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โมเดลการวัดและการวางตำแหน่ง”
ทำความเข้าใจเลย์เอาต์แบบผ่านครั้งเดียวของ Compose คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “โมเดลการวัดและการวางตำแหน่ง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โมเดลการวัดและการวางตำแหน่ง
- เขียน Layout Composable
- ข้อจำกัดและการวัดโดยธรรมชาติ
- FlowRow ที่ตัดชิปขึ้นบรรทัดใหม่