0Pricing
Jetpack Compose Academy · 课时

测量与放置模型

理解 Compose 的单遍布局过程。

测量与放置模型 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.width

Constraints 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 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。

「测量与放置模型」这节课中我会学到什么?

理解 Compose 的单遍布局过程。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Jetpack Compose Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「测量与放置模型」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?

能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 测量与放置模型
  2. 编写 Layout Composable
  3. 约束与固有尺寸测量
  4. 可换行芯片的 FlowRow
← 返回 Jetpack Compose Academy