将 XML 主题迁移到 Compose
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在现有应用中引入 Compose 时,您需要迁移 XML 中的主题才能对 Compose 界面使用 MaterialTheme
。这意味着应用的主题将会有 2 个可信来源:基于 View 的主题以及 Compose 主题。样式上的任何更改都需要在多处实施。将应用完全迁移到 Compose 后,您可以移除 XML 主题设置。
如需将 XML 主题迁移到 Compose,请使用 Material 主题构建器将 XML 主题迁移到 Compose 中的 Material 3。您可以使用现有的颜色角色(例如 XML 主题中的原色和间色),并将它们传递给 Material 主题构建器。这会在 Compose 中创建一个完整的 Material 3 主题,并提供可在应用中使用的可下载颜色和主题文件。
Material Theme Builder 会为您的应用生成 MaterialTheme
以及浅色和深色配色方案。如果您的应用使用自定义形状或排版,请分别通过定义 Shape
和 Typography
来迁移自定义形状和排版。确定后,请将该信息提供给您的 MaterialTheme
。如需了解详情,请参阅形状和排版。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Migrate XML themes to Compose\n\nWhen you introduce Compose in an existing app, you need to migrate your themes in XML to use `MaterialTheme` for Compose screens. This means your app's theming will have two sources of truth: the View-based theme and the Compose theme. Any changes to your styling need to be made in multiple places. Once your app is fully migrated to Compose, you can remove your XML theming.\n| **Note:** For non-Material design systems, see [Custom design systems in Compose](/develop/ui/compose/designsystems/custom).\n\nTo migrate your XML themes to Compose, use the [Material Theme Builder](https://m3.material.io/theme-builder) to migrate from an XML theme to [Material 3](/develop/ui/compose/designsystems/material3#material-theming) in Compose. You can use your existing color roles, such as primary and secondary colors from your XML theme, and pass them to the Material Theme Builder. This creates a fully Material 3 theme in Compose and provides downloadable color and theme files to use in your app.\n\nMaterial Theme Builder generates a `MaterialTheme` and light and dark color schemes for your app. If your app uses custom shapes or typography, migrate your custom shapes and typography by defining a `Shape` and `Typography`, respectively. Once defined, provide that information to your `MaterialTheme`. See [shapes](/develop/ui/compose/designsystems/material3#shapes) and [typography](/develop/ui/compose/designsystems/material3#typography) to learn more.\n| **Note:** If you are not using Material 3, see [Material Design 2 in Compose](/develop/ui/compose/designsystems/material) to learn how to create a theme. See [Migrate from Material 2 to Material 3 in Compose](/develop/ui/compose/designsystems/material2-material3) when you are ready to migrate to Material 3."]]