【问题标题】:How should i convert my AppTheme style to Material component我应该如何将我的 AppTheme 样式转换为 Material 组件
【发布时间】:2020-07-15 18:34:37
【问题描述】:

//这是我当前应用程序的 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">

【问题讨论】:

    标签: androidx material-components-android app-themes


    【解决方案1】:

    为了使用材料组件,首先在你的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 
    

    更多信息可以查看this page

    例如:

        <!-- 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>
    

    希望这会有所帮助! :)

    【讨论】:

    • 那行不通。应用程序在 activity.getActionBar().show() 处崩溃,并出现 nullPointerException。上下文是 Activity 而不是 AppCompatActivity。
    • 如果你使用 Material 组件,你不能使用 AppCompat 上下文。我给你的答案就是你问题的答案,所以请批准。如果您仍然遇到问题,我建议您尝试使用 Google 搜索或在 StackOverflow 上提出新问题。干杯! :)
    猜你喜欢
    • 2023-01-17
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2015-05-23
    • 2021-04-17
    相关资源
    最近更新 更多