首页 > 解决方案 > 我应该如何将我的 AppTheme 样式转换为 Material 组件

问题描述

//这是我当前应用程序的 AppTheme。我应该如何将我的 AppTheme 迁移到 Material Components?我已将我的代码迁移到 AndroidX。

<style name="AppBaseTheme" parent="@android:style/Theme.Light">
        <item name="android:windowActionBar">true</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="android:actionBarSize">@dimen/actionbar_height</item>
        <item name="actionBarSize">@dimen/actionbar_height</item>
    </style>
    <!-- Base application theme. -->
    <style name="AppThemeMenu" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowActionBar">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

标签: androidxmaterial-components-androidapp-themes

解决方案


为了使用材料组件,首先在你的gradel文件中实现材料库:

implementation 'com.google.android.material:material:1.1.0'

然后,进入 style.xml 并将父级更改为以下材料父级之一:

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight
Theme.MaterialComponents.DayNight.NoActionBar
Theme.MaterialComponents.DayNight.DarkActionBar 

有关更多信息,您可以查看此页面

例如:

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

希望这可以帮助!:)


推荐阅读