Android CardView 应用主题

Wir*_*ing 6 android android-layout android-theme android-support-library android-cardview

有没有办法将主题应用于 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>
Run Code Online (Sandbox Code Playgroud)

还有我的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>
Run Code Online (Sandbox Code Playgroud)

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

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

<item name="Cardview">@style/Custom.Widget.CardView</item>
Run Code Online (Sandbox Code Playgroud)

J-J*_*met 5

有可能,我也是找了好久才找到。这里是如果适用的橙色背景的所有cardview为例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>
Run Code Online (Sandbox Code Playgroud)