android,如何在使用Theme.AppCompat.Light.DarkActionBar时使用MaterialCardView

标签 android android-night-mode materialcardview

该应用程序使用 androidX 和 minSdk 21,并使用

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

最佳答案

尝试在您的 MaterialCardView 中使用它吗?

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

我相信将其添加到您的 MaterialCardView 中不会影响您的应用主题。

关于android,如何在使用Theme.AppCompat.Light.DarkActionBar时使用MaterialCardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60151974/

相关文章:

android - 如何将 ImageView 从一个 Activity 发送到另一个 Activity

android - fragment 中的 getDelegate() 方法?

android - 让 Android 上的 WebView 使用 prefers-color-scheme : dark

android - MaterialCardView:无法在 xml 布局中设置选中状态

android - 为 MaterialCardView 设置 "outside"底部边缘无法按预期工作

android - 如何在页面上禁用 AdMob

android - 如何为来自同一站点的这些文章设置默认选择器?

android - 从 ImageView 获取图像

Android 在主题为深色时更改品牌启动背景颜色