1

该应用程序正在使用带有 minSdk 21 的 androidX,并使用

AppCompatActivity有主题:

<style name="myAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/theme_colorPrimary</item>
        <item name="colorPrimaryDark">@color/theme_colorPrimaryDark</item>
        <item name="colorAccent">@color/theme_colorAccent</item>
    </style>

对于 AppCompat DayNight 主题,并切换白天或夜晚主题,例如:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) // or AppCompatDelegate.MODE_NIGHT_NO

但是当使用com.google.android.material.card.MaterialCardView (with implementation 'com.google.android.material:material:1.1.0')

<com.google.android.material.card.MaterialCardView
        style="@style/myCardViewStyle"
        ...>

    </com.google.android.material.card.MaterialCardView>

...

  <style name="CustomCardViewStyle" parent="@style/Widget.MaterialComponents.CardView">
     <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay_card_custom_corners</item>
  </style>

<style name="ShapeAppearanceOverlay_card_custom_corners" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">8dp</item>
    <item name="cornerSizeBottomLeft">8dp</item>
  </style>

它崩溃了:

android.view.InflateException: Binary XML file line #573: Binary XML file line #573: 
Error inflating class com.google.android.material.card.MaterialCardView

可能是什么原因?如果使用 MaterialCardView,应该使用什么主题并且仍然可以做

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

笔记:

Getting started with Material Components for Android中,它说Change your app theme to inherit from a Material Components theme,并且

Material Components themes
The following is the list of Material Components themes you can use to get the latest component styles and theme-level attributes.

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
Update your app theme to inherit from one of these themes, e.g.:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

我看到Theme.AppCompat.Light.DarkActionBar应用程序中使用的是派生链最终备份到android:Theme.Material.Light.NoActionBar

<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>
<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light">
<style name="Base.Theme.AppCompat.Light" parent="Base.V21.Theme.AppCompat.Light"/>
<style name="Base.V21.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light">
<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
<style name="Platform.AppCompat.Light" parent="Platform.V21.AppCompat.Light"/>

<style name="Platform.V21.AppCompat.Light" parent="android:Theme.Material.Light.NoActionBar">

并且Theme.MaterialComponents.Light.DarkActionBar被剥夺了android:Theme.Material.Light.NoActionBar

4

1 回答 1

2

尝试在您的 MaterialCardView 中使用它?

android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"

我相信在您的 MaterialCardView 中添加它不会影响您的应用主题。

于 2020-04-29T15:09:52.907 回答