android, как использовать MaterialCardView при использовании Theme.AppCompat.Light.DarkActionBar - PullRequest
1 голос
/ 10 февраля 2020

Приложение использует 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.MaterialCardViewimplementation '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>

, он получает cra sh:

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)

Примечание:

В Начало работы с компонентами материала для 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

1 Ответ

0 голосов
/ 29 апреля 2020

Попробуйте использовать это в вашем MaterialCardView?

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

Добавление этого в ваш MaterialCardView, я полагаю, не повлияет на тему вашего приложения.

...