Android 将自定义CardView样式附加到主题
在我的应用程序中,我有两个主题(浅色和深色),我希望所有的Android 将自定义CardView样式附加到主题,android,android-5.0-lollipop,android-cardview,Android,Android 5.0 Lollipop,Android Cardview,在我的应用程序中,我有两个主题(浅色和深色),我希望所有的cardwiews根据选择的主题更改其背景色 我不想要的是: <android.support.v7.widget.CardView style="@style/CardView.MyBlue" android:layout_width="200dp" android:layout_height="100dp" android:layout_gravity="center_horizontal">
cardwiew
s根据选择的主题更改其背景色
我不想要的是:
<android.support.v7.widget.CardView
style="@style/CardView.MyBlue"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal">
更新
类似的问题也没有答案。我能想到的唯一解决方案是手动保持一个恒定的变量,因为卡片颜色应该在每次主题更改之后。然后,我获取应用程序中的每个卡片实例,并将其颜色设置为该常量变量 因此,我有一个BaseActivity,我的所有活动都是从这个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);
}
}
这肯定不是最好的方法,但在其他人找到更好的方法来实现样式之前,我只能想到这一点。In style.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>
要详细说明如何实施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>
@style/cardwiew.Light
@style/CardView.Dark
然后,将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>
@style/cardwiew.Light
@style/CardView.Dark
然后在任何有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中使用该子类,而不是cardwiew
package com.example.widget;
导入android.content.Context;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
导入android.support.v7.widget.CardView;
导入android.util.AttributeSet;
导入com.example.R;
/**
*{@link cardwiew}子类,允许通过
*{@link R.attr#cardViewStyle}属性。
*
*该属性需要定义,例如在attrs.xml
中定义为:
*
您需要像往常一样在主题中设置该属性:
@style/cardwiew.MyStyle
并定义样式本身,例如:
0dp
非常有魅力!确保将CardView.Light放入灯光样式中,以便在您切换回FORTUSEfull时,它会将其拾取,但是如果有一个属性可以自动设置所有CardView的样式,而不为每个视图定义“样式”,该属性又如何呢?类似RadioButton的东西单选按钮样式。我看了看CardView的R.Attr,但它似乎不存在。
真的是必要的吗?还是将
放在样式中。xml
放在
速记下?@ARLabscardwiew
将0作为defstylettr
传递,所以我看到的唯一方法是将其子类化,并将自定义属性作为默认值传递。有关详细信息,请参阅我的答案。@ARLabs正在尝试实现相同的目标。怀疑这可能不可能,因为CardView位于核心Android SDK之外的库中
<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>
<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>
<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>
<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>