简体   繁体   English

Android CardView 应用主题

[英]Android CardView apply Theme

Is there a way to apply a theme to a Cardview?有没有办法将主题应用于 Cardview? I don't want to apply the style to every single view i'm creating.我不想将样式应用于我正在创建的每个视图。

This is my CardView right now:这是我现在的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>

And my styles.xml还有我的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>

I would like to add the style to themes.xml so that it will be applied to every Cardview, but I don't know how.我想将样式添加到themes.xml以便将其应用于每个 Cardview,但我不知道如何。 Is it even possible for views from the support library?支持库中的视图甚至可能吗?

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

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

It's possible, I also took a long time to find.有可能,我也是找了好久才找到。 Here is an example that applies an orange background to all cardview if the MyStyle style is used throughout the app.这里是如果myStyle的风格在整个应用程序中使用适用的橙色背景的所有cardview的例子。

In themes.xml :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>

我也在我的项目中使用了这个,没问题,可能你没有在 build.gradle 中添加这个

compile 'com.android.support:cardview-v7:25.2.0'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM