如需自定义主题,您可以将 colors 传递给 GlanceTheme。一览
提供 androidx.glance:glance-material 互操作性库,以便实现
Material 2 和 androidx.glance:glance-material3(适用于 Material 3 颜色)
联系。
例如,向 ColorProviders 提供应用的现有 Material 颜色。
创建 Glance 配色方案的 API,如以下代码段所示:
// Remember, use the Glance imports// import androidx.glance.material3.ColorProviders// Example Imports from your own app// import com.example.myapp.ui.theme.DarkColors// import com.example.myapp.ui.theme.LightColorsobjectMyAppWidgetGlanceColorScheme{valcolors=ColorProviders(light=LightColors,dark=DarkColors)}
[[["易于理解","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"]],["最后更新时间 (UTC):2025-08-25。"],[],[],null,["Glance provides an API to manage the color theme. For other style attributes,\nsuch as [`TextStyle`](/reference/kotlin/androidx/compose/ui/text/TextStyle), declare top-level variables.\n\nAdd colors\n\nGlance provides an implementation of Material colors out of the box. To use the\nbuilt-in theme, wrap your top level composable with `GlanceTheme`, as shown in\nthe following example.\n\nOn devices that support dynamic colors, this theme is derived from the\nuser-specific platform colors. On other devices, this falls back to the Material\nbaseline theme. Use `GlanceTheme.colors` to style with colors from the wrapped\ntheme. You can use these values from the theme anywhere a color is needed.\n\n\n```kotlin\noverride suspend fun provideGlance(context: Context, id: GlanceId) {\n\n provideContent {\n GlanceTheme {\n MyContent()\n }\n }\n}\n\n@Composable\nprivate fun MyContent() {\n\n Image(\n colorFilter = ColorFilter.tint(GlanceTheme.colors.secondary),\n // ...\n\n )\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt#L756-L777\n```\n\n\u003cbr /\u003e\n\nTo customize the theme, you can pass the `colors` to the `GlanceTheme`. Glance\nprovides the `androidx.glance:glance-material` interoperability library for\nMaterial 2, and `androidx.glance:glance-material3` for Material 3 colors\nsupport.\n\nFor example, provide your app's existing material colors to the `ColorProviders`\nAPI to create a Glance color scheme, as shown in the following snippet:\n\n\n```kotlin\n// Remember, use the Glance imports\n// import androidx.glance.material3.ColorProviders\n\n// Example Imports from your own app\n// import com.example.myapp.ui.theme.DarkColors\n// import com.example.myapp.ui.theme.LightColors\n\nobject MyAppWidgetGlanceColorScheme {\n\n val colors = ColorProviders(\n light = LightColors,\n dark = DarkColors\n )\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt#L784-L797\n```\n\n\u003cbr /\u003e\n\nProvide the colors from the scheme to the `GlanceTheme` that wraps all your\ncomposables, as shown in the following example:\n\n\n```kotlin\noverride suspend fun provideGlance(context: Context, id: GlanceId) {\n // ...\n\n provideContent {\n GlanceTheme(colors = MyAppWidgetGlanceColorScheme.colors) {\n MyContent()\n }\n }\n}\n\n@Composable\nprivate fun MyContent() {\n\n Image(\n colorFilter = ColorFilter.tint(GlanceTheme.colors.secondary),\n // ...\n )\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt#L800-L820\n```\n\n\u003cbr /\u003e\n\nIf you prefer to use dynamic colors from the wallpaper when supported, and your\napp's color scheme otherwise, you can conditionally pass your app's color scheme\nin the `GlanceTheme`. This is shown in the following snippet:\n\n\n```kotlin\noverride suspend fun provideGlance(context: Context, id: GlanceId) {\n\n provideContent {\n GlanceTheme(\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.S)\n GlanceTheme.colors\n else\n MyAppWidgetGlanceColorScheme.colors\n ) {\n MyContent()\n }\n }\n}\n\n@Composable\nprivate fun MyContent() {\n // ...\n Image(\n colorFilter = ColorFilter.tint(GlanceTheme.colors.secondary),\n // ...\n )\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt#L834-L858\n```\n\n\u003cbr /\u003e\n\nAdd shapes\n\nTo provide special shapes or shadows to your app widget, use the Android\nDrawables API.\n\nFor example, the following snippet shows how to create a drawable (a shape): \n\n \u003cshape xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003ccorners android:radius=\"16dp\"/\u003e\n \u003cstroke android:color=\"@color/outline_color\" android:width=\"1dp\"/\u003e\n \u003c/shape\u003e \n https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/res/drawable/button_outline.xml#L18-L21\n\nProvide it to the target composable:\n\n\n```kotlin\nGlanceModifier.background(\n imageProvider = ImageProvider(R.drawable.button_outline)\n)https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt#L866-L868\n```\n\n\u003cbr /\u003e\n\n| **Note:** You can use the Android resource folder structure to define different shapes or other resources for any type of configuration (e.g., `values-night`)."]]