เช่น ระบุสีวัสดุที่มีอยู่ของแอปให้กับ ColorProviders
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"]],["อัปเดตล่าสุด 2025-08-28 UTC"],[],[],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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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/7a0ebbee11495f628cf9d574f6b6069c2867232a/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/dd30aee903e8c247786c064faab1a9ca8d10b46e/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`)."]]