【问题标题】:android, how to use MaterialCardView while using Theme.AppCompat.Light.DarkActionBarandroid,如何在使用 Theme.AppCompat.Light.DarkActionBar 时使用 MaterialCardView
【发布时间】:2020-05-25 20:44:35
【问题描述】:

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

【问题讨论】:

标签: android android-night-mode materialcardview


【解决方案1】:

尝试在您的 MaterialCardView 中使用它?

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

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

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2021-11-25
    • 1970-01-01
    • 2020-07-11
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    相关资源
    最近更新 更多