如何从主题更改CardView背景颜色 [英] How to Change CardView background color from a theme
本文介绍了如何从主题更改CardView背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在具有多个主题的应用中,如何从样式文件中更改CardView的背景颜色?
In an app that has multiple themes, how would you change the background color of the CardView from the styles file?
styles.xml
styles.xml
<style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryTheme1</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkTheme1</item>
<item name="colorAccent">@color/colorAccentTheme1</item>
</style>
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryTheme2</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkTheme2</item>
<item name="colorAccent">@color/colorAccentTheme2</item>
</style>
<style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryTheme3</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkTheme3</item>
<item name="colorAccent">@color/colorAccentTheme3</item>
</style>
CardView
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:elevation="4dp"
app:cardCornerRadius="15dp">
我想更改每个主题的背景色,希望在styles.xml文件中完成此操作.
I want to change the background color for each theme and I'm hoping to accomplish this in the styles.xml file.
推荐答案
尝试以下代码行
在style.xml中编写以下代码行
<style name="CustomCardview1" parent="CardView">
<item name="cardBackgroundColor">#F3921F</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">10dp</item>
</style>
<style name="CustomCardview2" parent="CardView">
<item name="cardBackgroundColor">#F3931F</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">10dp</item>
</style>
在需要卡片视图的布局中编写以下代码行
<android.support.v7.widget.CardView
style="@style/CustomCardview">
希望对您有帮助
这篇关于如何从主题更改CardView背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文