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