@ComposablefunMyCustomComposable(modifier:Modifier=Modifier,content:@Composable()->Unit){Layout(content=content,modifier=modifier,measurePolicy=object:MeasurePolicy{overridefunMeasureScope.measure(measurables:List<Measurable>,constraints:Constraints):MeasureResult{// Measure and layout here// ...}overridefunIntrinsicMeasureScope.minIntrinsicWidth(measurables:List<IntrinsicMeasurable>,height:Int):Int{// Logic here// ...}// Other intrinsics related methods have a default value,// you can override only the methods that you need.})}
funModifier.myCustomModifier(/* ... */)=thisthenobject:LayoutModifier{overridefunMeasureScope.measure(measurable:Measurable,constraints:Constraints):MeasureResult{// Measure and layout here// ...}overridefunIntrinsicMeasureScope.minIntrinsicWidth(measurable:IntrinsicMeasurable,height:Int):Int{// Logic here// ...}// Other intrinsics related methods have a default value,// you can override only the methods that you need.}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-28 (世界標準時間)。"],[],[],null,["One of the rules of Compose is that you should only measure your children once;\nmeasuring children twice throws a runtime exception. However, there are times\nwhen you need some information about your children before measuring them.\n\n**Intrinsics lets you query children before they're actually measured.**\n\nTo a composable, you can ask for its `IntrinsicSize.Min` or `IntrinsicSize.Max`:\n\n- `Modifier.width(IntrinsicSize.Min)` - What's the minimum width you need to display your content properly?\n- `Modifier.width(IntrinsicSize.Max)` - What's the maximum width you need to display your content properly?\n- `Modifier.height(IntrinsicSize.Min)` - What's the minimum height you need to display your content properly?\n- `Modifier.height(IntrinsicSize.Max)` - What's the maximum height you need to display your content properly?\n\nFor example, if you ask the `minIntrinsicHeight` of a `Text` with infinite\n`width` constraints in a custom layout, it'll return the `height` of the `Text`\nwith the text drawn in a single line.\n| **Note:** Asking for intrinsics measurements doesn't measure the children twice. Children are queried for their intrinsic measurements before they're measured and then, based on that information the parent calculates the constraints to measure its children with.\n\nIntrinsics in action\n\nImagine that we want to create a composable that displays two texts on the\nscreen separated by a divider like this:\n\nHow can we do this? We can have a `Row` with two `Text`s inside that expands as\nmuch as they can and a `Divider` in the middle. We want the `Divider` to be as\ntall as the tallest `Text` and thin (`width = 1.dp`).\n\n\n```kotlin\n@Composable\nfun TwoTexts(modifier: Modifier = Modifier, text1: String, text2: String) {\n Row(modifier = modifier) {\n Text(\n modifier = Modifier\n .weight(1f)\n .padding(start = 4.dp)\n .wrapContentWidth(Alignment.Start),\n text = text1\n )\n VerticalDivider(\n color = Color.Black,\n modifier = Modifier.fillMaxHeight().width(1.dp)\n )\n Text(\n modifier = Modifier\n .weight(1f)\n .padding(end = 4.dp)\n .wrapContentWidth(Alignment.End),\n\n text = text2\n )\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt#L64-L87\n```\n\n\u003cbr /\u003e\n\nIf we preview this, we see that the `Divider` expands to the whole screen and\nthat's not what we want:\n\nThis happens because `Row` measures each child individually and the height of\n`Text` cannot be used to constraint the `Divider`. We want the `Divider` to fill\nthe available space with a given height. For that, we can use the\n`height(IntrinsicSize.Min)` modifier .\n\n`height(IntrinsicSize.Min)` sizes its children being forced to be as tall as\ntheir minimum intrinsic height. As it's recursive, it'll query `Row` and its\nchildren `minIntrinsicHeight`.\n\nApplying that to our code, it'll work as expected:\n\n\n```kotlin\n@Composable\nfun TwoTexts(modifier: Modifier = Modifier, text1: String, text2: String) {\n Row(modifier = modifier.height(IntrinsicSize.Min)) {\n Text(\n modifier = Modifier\n .weight(1f)\n .padding(start = 4.dp)\n .wrapContentWidth(Alignment.Start),\n text = text1\n )\n VerticalDivider(\n color = Color.Black,\n modifier = Modifier.fillMaxHeight().width(1.dp)\n )\n Text(\n modifier = Modifier\n .weight(1f)\n .padding(end = 4.dp)\n .wrapContentWidth(Alignment.End),\n\n text = text2\n )\n }\n}\n\n// @Preview\n@Composable\nfun TwoTextsPreview() {\n MaterialTheme {\n Surface {\n TwoTexts(text1 = \"Hi\", text2 = \"there\")\n }\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt#L104-L137\n```\n\n\u003cbr /\u003e\n\nWith preview:\n\nThe `Row` composable's `minIntrinsicHeight` will be the maximum\n`minIntrinsicHeight` of its children. The `Divider` element's\n`minIntrinsicHeight` is 0 as it doesn't occupy space if no constraints are\ngiven; the `Text` `minIntrinsicHeight` will be that of the text given a specific\n`width`. Therefore, the `Row` element's `height` constraint will be the max\n`minIntrinsicHeight` of the `Text`s. `Divider` will then expand its `height` to\nthe `height` constraint given by the `Row`.\n\nIntrinsics in your custom layouts\n\nWhen creating a custom `Layout` or `layout` modifier, intrinsic measurements\nare calculated automatically based on approximations. Therefore, the\ncalculations might not be correct for all layouts. These APIs offer options\nto override these defaults.\n\nTo specify the intrinsics measurements of your custom `Layout`,\noverride the `minIntrinsicWidth`, `minIntrinsicHeight`, `maxIntrinsicWidth`,\nand `maxIntrinsicHeight` of the\n[`MeasurePolicy`](/reference/kotlin/androidx/compose/ui/layout/MeasurePolicy)\ninterface when creating it.\n\n\n```kotlin\n@Composable\nfun MyCustomComposable(\n modifier: Modifier = Modifier,\n content: @Composable () -\u003e Unit\n) {\n Layout(\n content = content,\n modifier = modifier,\n measurePolicy = object : MeasurePolicy {\n override fun MeasureScope.measure(\n measurables: List\u003cMeasurable\u003e,\n constraints: Constraints\n ): MeasureResult {\n // Measure and layout here\n // ...\n }\n\n override fun IntrinsicMeasureScope.minIntrinsicWidth(\n measurables: List\u003cIntrinsicMeasurable\u003e,\n height: Int\n ): Int {\n // Logic here\n // ...\n }\n\n // Other intrinsics related methods have a default value,\n // you can override only the methods that you need.\n }\n )\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt#L143-L176\n```\n\n\u003cbr /\u003e\n\nWhen creating your custom `layout` modifier, override the related methods\nin the `LayoutModifier` interface.\n\n\n```kotlin\nfun Modifier.myCustomModifier(/* ... */) = this then object : LayoutModifier {\n\n override fun MeasureScope.measure(\n measurable: Measurable,\n constraints: Constraints\n ): MeasureResult {\n // Measure and layout here\n // ...\n }\n\n override fun IntrinsicMeasureScope.minIntrinsicWidth(\n measurable: IntrinsicMeasurable,\n height: Int\n ): Int {\n // Logic here\n // ...\n }\n\n // Other intrinsics related methods have a default value,\n // you can override only the methods that you need.\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/layouts/IntrinsicSnippets.kt#L182-L206\n```\n\n\u003cbr /\u003e\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Custom layouts {:#custom-layouts }](/develop/ui/compose/layouts/custom)\n- [Alignment lines in Jetpack Compose](/develop/ui/compose/layouts/alignment-lines)\n- [Jetpack Compose Phases](/develop/ui/compose/phases)"]]