该应用程序正在使用带有 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