Android CardView 应用主题

Android CardView apply Theme

有没有办法将主题应用到 Cardview?我不想将样式应用于我正在创建的每个视图。

这是我现在的 CardView

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Custom.Widget.CardView">
    <-- I don't want to define the style every time -->
</android.support.v7.widget.CardView>

还有我的styles.xml

<style name="Custom.Widget.CardView" parent="CardView">
    <item name="cardBackgroundColor">@color/card_backgroundColor</item>
    <item name="cardCornerRadius">12dp</item>
    <item name="cardUseCompatPadding">true</item>
    <item name="contentPadding">4dp</item>
</style>

我想将样式添加到themes.xml,以便它可以应用于每个Cardview,但我不知道如何。甚至可以从支持库中获取视图吗?

当我将以下内容添加到 themes.xml 时,我收到警告:no resource found that matches the given name: attr 'Cardview'

<item name="Cardview">@style/Custom.Widget.CardView</item>

我也在我的项目中使用它,没问题,可能是你没有在你的 build.gradle

中添加它
compile 'com.android.support:cardview-v7:25.2.0'

有可能,我也找了很久。如果在整个应用程序中使用 MyStyle 样式,则以下示例将橙色背景应用于所有卡片视图。

themes.xml:

<style name="MyStyle" parent="Theme.AppCompat">
    ...

    <item name="cardViewStyle">@style/MyStyle.Cardview.Orange</item>
</style>

<!-- CardView -->
<style name="MyStyle.Cardview.Orange" parent="CardView">
    <item name="cardBackgroundColor">#F3921F</item>
</style>