Compose 主題剖析
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Jetpack Compose 的主題是由多個較低層級的建構和相關 API 組成。您可以在 MaterialTheme
的原始碼中查看這些套件,也可在自訂設計系統中套用。
主題系統類別
主題通常由多個子系統組成,這些子系統結合了共同的視覺元素和
行為概念這些系統可以使用
主題化值
舉例來說,MaterialTheme
包含
ColorScheme
(色彩系統)、
Typography
(字體排版系統) 和
Shapes
(形狀系統)。
@Immutable
data class ColorSystem(
val color: Color,
val gradient: List<Color>
/* ... */
)
@Immutable
data class TypographySystem(
val fontFamily: FontFamily,
val textStyle: TextStyle
)
/* ... */
@Immutable
data class CustomSystem(
val value1: Int,
val value2: String
/* ... */
)
/* ... */
主題系統 Composition Local
主題系統類別會以隱含形式提供給組合樹,如
CompositionLocal
敬上
執行個體。這可讓可組合函式中的主題值採用靜態參照。
如要進一步瞭解「CompositionLocal
」,請參閱:
使用 CompositionLocal 指南的本機範圍資料。
val LocalColorSystem = staticCompositionLocalOf {
ColorSystem(
color = Color.Unspecified,
gradient = emptyList()
)
}
val LocalTypographySystem = staticCompositionLocalOf {
TypographySystem(
fontFamily = FontFamily.Default,
textStyle = TextStyle.Default
)
}
val LocalCustomSystem = staticCompositionLocalOf {
CustomSystem(
value1 = 0,
value2 = ""
)
}
/* ... */
主題功能
主題函式為進入點和主要 API,容器會建構執行個體
主題系統 CompositionLocal
- 使用實際值任何邏輯
必要 — 提供給具有
CompositionLocalProvider
。
content
參數允許巢狀的可組合項存取階層相對的主題值。
@Composable
fun Theme(
/* ... */
content: @Composable () -> Unit
) {
val colorSystem = ColorSystem(
color = Color(0xFF3DDC84),
gradient = listOf(Color.White, Color(0xFFD7EFFF))
)
val typographySystem = TypographySystem(
fontFamily = FontFamily.Monospace,
textStyle = TextStyle(fontSize = 18.sp)
)
val customSystem = CustomSystem(
value1 = 1000,
value2 = "Custom system"
)
/* ... */
CompositionLocalProvider(
LocalColorSystem provides colorSystem,
LocalTypographySystem provides typographySystem,
LocalCustomSystem provides customSystem,
/* ... */
content = content
)
}
主題物件
您可以使用具有便利屬性的物件來存取主題系統。為保持一致性,物件通常會採用與主題函式相同的名稱。屬性只會取得本機目前的組合。
// Use with eg. Theme.colorSystem.color
object Theme {
val colorSystem: ColorSystem
@Composable
get() = LocalColorSystem.current
val typographySystem: TypographySystem
@Composable
get() = LocalTypographySystem.current
val customSystem: CustomSystem
@Composable
get() = LocalCustomSystem.current
/* ... */
}
為您推薦
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[[["容易理解","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-27 (世界標準時間)。"],[],[],null,["Themes in Jetpack Compose are made up of a number of lower-level constructs\nand related APIs. These can be seen in the\n[source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/MaterialTheme.kt)\nof `MaterialTheme` and can also be applied in custom design systems.\n\nTheme system classes\n\nA theme is typically made up of a number of subsystems that group common visual and\nbehavioral concepts. These systems can be modeled with classes which have\ntheming values.\n\nFor example, `MaterialTheme` includes\n[`ColorScheme`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/ColorScheme.kt)\n(color system),\n[`Typography`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Typography.kt)\n(typography system), and\n[`Shapes`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Shapes.kt)\n(shape system).\n| **Note:** Classes should be annotated with [`Stable`](/reference/kotlin/androidx/compose/runtime/Stable) or [`@Immutable`](/reference/kotlin/androidx/compose/runtime/Immutable) to provide information to the Compose compiler. To learn more, check out the [Lifecycle of composables guide](/develop/ui/compose/lifecycle#skipping).\n\n\n```kotlin\n@Immutable\ndata class ColorSystem(\n val color: Color,\n val gradient: List\u003cColor\u003e\n /* ... */\n)\n\n@Immutable\ndata class TypographySystem(\n val fontFamily: FontFamily,\n val textStyle: TextStyle\n)\n/* ... */\n\n@Immutable\ndata class CustomSystem(\n val value1: Int,\n val value2: String\n /* ... */\n)\n\n/* ... */https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/designsystems/ThemeAnatomySnippets.kt#L31-L52\n```\n\n\u003cbr /\u003e\n\nTheme system composition locals\n\nTheme system classes are implicitly provided to the composition tree as\n[`CompositionLocal`](/reference/kotlin/androidx/compose/runtime/CompositionLocal)\ninstances. This allows theming values to be statically referenced in composable\nfunctions.\n\nTo learn more about `CompositionLocal`, check out the\n[Locally scoped data with CompositionLocal guide](/develop/ui/compose/compositionlocal).\n| **Note:** You can create a class's `CompositionLocal` with [`compositionLocalOf`](/reference/kotlin/androidx/compose/runtime/package-summary#compositionlocalof) or [`staticCompositionLocalOf`](/reference/kotlin/androidx/compose/runtime/package-summary#staticcompositionlocalof). These functions have a `defaultFactory` trailing lambda to provide fallback values of the same type that they're providing. It's a good idea to use reasonable defaults like `Color.Unspecified`, `TextStyle.Default`, etc.\n\n\n```kotlin\nval LocalColorSystem = staticCompositionLocalOf {\n ColorSystem(\n color = Color.Unspecified,\n gradient = emptyList()\n )\n}\n\nval LocalTypographySystem = staticCompositionLocalOf {\n TypographySystem(\n fontFamily = FontFamily.Default,\n textStyle = TextStyle.Default\n )\n}\n\nval LocalCustomSystem = staticCompositionLocalOf {\n CustomSystem(\n value1 = 0,\n value2 = \"\"\n )\n}\n\n/* ... */https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/designsystems/ThemeAnatomySnippets.kt#L56-L77\n```\n\n\u003cbr /\u003e\n\nTheme function\n\nThe theme function is the entry point and primary API. It constructs instances\nof the theme system `CompositionLocal`s --- using real values any logic\nrequired --- that are provided to the composition tree with\n[`CompositionLocalProvider`](/reference/kotlin/androidx/compose/runtime/package-summary#compositionlocalprovider).\nThe `content` parameter allows nested composables to access theming values\nrelative to the hierarchy.\n\n\n```kotlin\n@Composable\nfun Theme(\n /* ... */\n content: @Composable () -\u003e Unit\n) {\n val colorSystem = ColorSystem(\n color = Color(0xFF3DDC84),\n gradient = listOf(Color.White, Color(0xFFD7EFFF))\n )\n val typographySystem = TypographySystem(\n fontFamily = FontFamily.Monospace,\n textStyle = TextStyle(fontSize = 18.sp)\n )\n val customSystem = CustomSystem(\n value1 = 1000,\n value2 = \"Custom system\"\n )\n /* ... */\n CompositionLocalProvider(\n LocalColorSystem provides colorSystem,\n LocalTypographySystem provides typographySystem,\n LocalCustomSystem provides customSystem,\n /* ... */\n content = content\n )\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/designsystems/ThemeAnatomySnippets.kt#L81-L106\n```\n\n\u003cbr /\u003e\n\nTheme object\n\nAccessing theme systems is done using an object with convenience properties. For\nconsistency, the object tends to be named the same as the theme function. The\nproperties simply get the current composition local.\n\n\n```kotlin\n// Use with eg. Theme.colorSystem.color\nobject Theme {\n val colorSystem: ColorSystem\n @Composable\n get() = LocalColorSystem.current\n val typographySystem: TypographySystem\n @Composable\n get() = LocalTypographySystem.current\n val customSystem: CustomSystem\n @Composable\n get() = LocalCustomSystem.current\n /* ... */\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/designsystems/ThemeAnatomySnippets.kt#L110-L122\n```\n\n\u003cbr /\u003e\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Locally scoped data with CompositionLocal](/develop/ui/compose/compositionlocal)\n- [Custom design systems in Compose](/develop/ui/compose/designsystems/custom)\n- [Material Design 3 in Compose](/develop/ui/compose/designsystems/material3)"]]