将自定义CardView样式附加到主题

sam*_*oes 21 android android-cardview android-5.0-lollipop

在我的应用程序中,我有两个主题(浅色和深色),我希望我CardView的所有s都根据所选的主题更改其背景颜色.

想要的是:

<android.support.v7.widget.CardView
    style="@style/CardView.MyBlue"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_gravity="center_horizontal">
Run Code Online (Sandbox Code Playgroud)

上面的代码不是动态的.我需要自动应用我的两种样式,具体取决于是选择了光照还是黑暗主题.


我现在拥有的东西不起作用:

<style name="AppTheme.Light" parent="Theme.AppCompat.Light">
   ...
   <item name="cardViewStyle">@style/CardViewStyle.Light</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
   ...
   <item name="cardViewStyle">@style/CardViewStyle.Dark</item>
</style>

<style name="CardViewStyle.Light" parent="CardView">
    <item name="cardBackgroundColor">@color/cardview_dark_background</item>
</style>

<style name="CardViewStyle.Dark" parent="CardView">
        <item name="cardBackgroundColor">@color/cardview_light_background</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我在某处读到你可以定义一个styleable.xml文件/res/values/来键入单词cardViewStyle,所以我这样做:

styleable.xml:

<resources>
    <declare-styleable name="AppTheme">
        <attr name="cardViewStyle" format="reference" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

更新

类似的问题在这里也没有答案.

Dav*_*ark 19

在styles.xml中

<resources>

    <attr format="reference" name="cardStyle"/>

    <style name="Light" parent="Theme.AppCompat.NoActionBar">
        <item name="cardStyle">@style/CardView.Light</item>
        ...
    </style>

    <style name="Dark" parent="Theme.AppCompat.NoActionBar">
        <item name="cardStyle">@style/CardView.Dark</item>
        ...
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后在你的其他xml中使用new属性,就像这样使用它

style="?attr/cardStyle"
Run Code Online (Sandbox Code Playgroud)


小智 8

要显示有关如何实施David Park解决方案的更多详细信息......

把它放在attrs.xml中:

<declare-styleable name = "cardStyle">
     <attr name="cardStyle" format="reference" />
</declare-styleable>

<style name="Light" parent="Theme.AppCompat.NoActionBar">
    <item name="cardStyle">@style/CardView.Light</item>
</style>

<style name="Dark" parent="Theme.AppCompat.NoActionBar">
    <item name="cardStyle">@style/CardView.Dark</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后,将cardStyle添加到styles.xml中的主题:

<style name="AppThemeLight" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="cardStyle">@style/CardView.Light</item>
</style>

<style name="AppThemeDark" parent="AppTheme.Base.Dark"/>
<style name="AppTheme.Base.Dark" parent="Theme.AppCompat.NoActionBar">
    <item name="cardStyle">@style/CardView.Dark</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在任何有CardView的地方使用attr:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/header_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp"
    style="?attr/cardStyle">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:id="@+id/header_title"

</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

  • 有用,但是如果没有为每个View定义"样式",那么自动显示所有CardView的属性呢?用于单选按钮的radioButtonStyle之类的东西.我看了看CardView的R.Attr,但它似乎不存在.http://developer.android.com/reference/android/support/v7/cardview/R.attr.html (2认同)