将自定义 CardView 样式附加到主题
Attach custom CardView style to theme
在我的应用程序中,我有两个主题(浅色和深色),我希望我的所有 CardView
都根据选择的主题更改它们的背景颜色。
我不想要的是:
<android.support.v7.widget.CardView
style="@style/CardView.MyBlue"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal">
以上代码不是动态的。我需要根据选择浅色或深色主题自动应用我的两种样式。
我现在有的东西不起作用:
<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>
我在某处读到,您可以在 /res/values/
中定义一个 styleable.xml
文件来键入单词 cardViewStyle
,所以我这样做了:
styleable.xml:
<resources>
<declare-styleable name="AppTheme">
<attr name="cardViewStyle" format="reference" />
</declare-styleable>
</resources>
更新
类似问题 here 也没有答案。
我能想出的唯一解决方案是在每次主题更改后手动保存卡片颜色的常量变量。然后,我获取应用程序中卡片的每个实例,并将其颜色设置为该常量变量。
所以,我有一个 BaseActivity,我的所有活动都从它扩展而来。在其中,除了处理主题外,我还处理卡片颜色变化。见下文:
BaseActivity.java
:
public class BaseActivity extends ActionBarActivity {
private static final int DEFAULT_THEME_ID = R.style.AppTheme_Dark;
@Override
protected void onCreate(Bundle savedInstanceState) {
int theme_id =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("THEME_ID", DEFAULT_THEME_ID);
setTheme(theme_id);
int card_color;
if (theme_id == DEFAULT_THEME_ID){
card_color = R.color.cv_dark;
} else{
card_color = R.color.cv_light;
}
Constants.CARD_COLOR = card_color;
super.onCreate(savedInstanceState);
}
}
这绝对不是最好的方法,但在其他人找到更好的方法来实现样式之前,这就是我能想到的。
在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>
然后在你的另一个xml中使用新属性,你会像这样使用它
style="?attr/cardStyle"
显示有关如何实施 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>
然后,将 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>
然后在任何有 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>
如果您不想将 style="?cardViewStyle"
添加到布局中的每个 CardView
,您可以继承 CardView
并在构造函数中设置样式属性(可惜他们没有' 在支持库中执行此操作)。然后在 XML 中使用该子类,而不是 CardView
.
package com.example.widget;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import com.example.R;
/**
* {@link CardView} subclass that allows theme'ing through
* the {@link R.attr#cardViewStyle} attribute.
* <p>
* The attribute needs to be defined, for example in <code>attrs.xml</code> as:
* <pre>
* <attr format="reference" name="cardViewStyle"/>
* </pre>
* <p>
* You'll need to set that attribute in your theme, as usual:
* <pre>
* <item name="cardViewStyle">@style/CardView.MyStyle</item>
* </pre>
* And define the style itself, for example:
* <pre>
* <style name="CardView.MyStyle" parent="CardView.Light">
* <item name="cardCornerRadius">0dp</item>
* </style>
* </pre>
*/
public class StylishCardView extends CardView {
public StylishCardView(@NonNull Context context) {
this(context, null);
}
public StylishCardView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.cardViewStyle);
}
public StylishCardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
需要定义属性cardViewStyle
,例如在attrs.xml
中为:
<attr format="reference" name="cardViewStyle"/>
您需要像往常一样在您的主题中设置该属性:
<item name="cardViewStyle">@style/CardView.MyStyle</item>
并定义样式本身,例如:
<style name="CardView.MyStyle" parent="CardView.Light">
<item name="cardCornerRadius">0dp</item>
</style>
在我的应用程序中,我有两个主题(浅色和深色),我希望我的所有 CardView
都根据选择的主题更改它们的背景颜色。
我不想要的是:
<android.support.v7.widget.CardView
style="@style/CardView.MyBlue"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal">
以上代码不是动态的。我需要根据选择浅色或深色主题自动应用我的两种样式。
我现在有的东西不起作用:
<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>
我在某处读到,您可以在 /res/values/
中定义一个 styleable.xml
文件来键入单词 cardViewStyle
,所以我这样做了:
styleable.xml:
<resources>
<declare-styleable name="AppTheme">
<attr name="cardViewStyle" format="reference" />
</declare-styleable>
</resources>
更新
类似问题 here 也没有答案。
我能想出的唯一解决方案是在每次主题更改后手动保存卡片颜色的常量变量。然后,我获取应用程序中卡片的每个实例,并将其颜色设置为该常量变量。
所以,我有一个 BaseActivity,我的所有活动都从它扩展而来。在其中,除了处理主题外,我还处理卡片颜色变化。见下文:
BaseActivity.java
:
public class BaseActivity extends ActionBarActivity {
private static final int DEFAULT_THEME_ID = R.style.AppTheme_Dark;
@Override
protected void onCreate(Bundle savedInstanceState) {
int theme_id =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("THEME_ID", DEFAULT_THEME_ID);
setTheme(theme_id);
int card_color;
if (theme_id == DEFAULT_THEME_ID){
card_color = R.color.cv_dark;
} else{
card_color = R.color.cv_light;
}
Constants.CARD_COLOR = card_color;
super.onCreate(savedInstanceState);
}
}
这绝对不是最好的方法,但在其他人找到更好的方法来实现样式之前,这就是我能想到的。
在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>
然后在你的另一个xml中使用新属性,你会像这样使用它
style="?attr/cardStyle"
显示有关如何实施 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>
然后,将 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>
然后在任何有 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>
如果您不想将 style="?cardViewStyle"
添加到布局中的每个 CardView
,您可以继承 CardView
并在构造函数中设置样式属性(可惜他们没有' 在支持库中执行此操作)。然后在 XML 中使用该子类,而不是 CardView
.
package com.example.widget;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import com.example.R;
/**
* {@link CardView} subclass that allows theme'ing through
* the {@link R.attr#cardViewStyle} attribute.
* <p>
* The attribute needs to be defined, for example in <code>attrs.xml</code> as:
* <pre>
* <attr format="reference" name="cardViewStyle"/>
* </pre>
* <p>
* You'll need to set that attribute in your theme, as usual:
* <pre>
* <item name="cardViewStyle">@style/CardView.MyStyle</item>
* </pre>
* And define the style itself, for example:
* <pre>
* <style name="CardView.MyStyle" parent="CardView.Light">
* <item name="cardCornerRadius">0dp</item>
* </style>
* </pre>
*/
public class StylishCardView extends CardView {
public StylishCardView(@NonNull Context context) {
this(context, null);
}
public StylishCardView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.cardViewStyle);
}
public StylishCardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
需要定义属性cardViewStyle
,例如在attrs.xml
中为:
<attr format="reference" name="cardViewStyle"/>
您需要像往常一样在您的主题中设置该属性:
<item name="cardViewStyle">@style/CardView.MyStyle</item>
并定义样式本身,例如:
<style name="CardView.MyStyle" parent="CardView.Light">
<item name="cardCornerRadius">0dp</item>
</style>