【问题标题】:MaterialCardview requires Theme.AppCompatMaterialCardview 需要 Theme.AppCompat
【发布时间】:2018-06-17 16:14:29
【问题描述】:

我正在尝试测试我在新的 Google Material 组件上的技能。 但是现在我遇到了MaterialCardView的问题

搭建过程告诉我

The style on this component requires your app theme to be Theme.AppCompat 
[..]
at com.google.android.material.card.MaterialCardView.<init>

有了这个线索,我添加了 style="@style/Theme.AppCompat" & android:theme="@style/Theme.AppCompat"MaterialCardView 以及清单中的我的活动。

我也尝试将我的 Acitivity 更改为 AppCompatActivity 但没有任何成功。

我也尝试设置 material.io 文档告诉的样式,但没有成功!

你有什么线索吗?

谢谢

【问题讨论】:

  • 你能分享你的膨胀代码吗?

标签: android themes material-design android-cardview


【解决方案1】:

根据 Material Components 1.2.1 你需要这样做:

  • 依赖库implementation 'com.google.android.material:material:1.2.1'
  • 您需要拥有compileSdkVersion 30
  • 您的活动需要扩展AppCompatActivity(或use AppCompatDelegate
  • 您必须使用 Material Components 主题

来源:https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md

最简单的入门方法是扩展您当前的主题 Theme.MaterialComponents.*.Bridge 而不是 Theme.AppCompat.*

此外,您需要覆盖主题中的以下属性,否则卡片颜色将为broken

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
    <item name="elevationOverlayEnabled">false</item>
</style>

不要在卡上设置android:theme="@style/Theme.MaterialComponents"。您将丢失卡片内每个小部件上主题的颜色信息(主要、次要、重音...)。

不要在卡上设置style="@style/Theme.MaterialComponentsDon't mix themes and styles.

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 2015-08-04
    • 2014-07-01
    相关资源
    最近更新 更多