如何从主题更改 CardView 背景颜色
How to Change CardView background color from a theme
在一个有多个主题的应用程序中,您将如何从样式文件更改 CardView 的背景颜色?
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>
卡片视图
<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 文件中完成。
尝试下面的代码行
在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 的背景颜色?
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>
卡片视图
<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 文件中完成。
尝试下面的代码行
在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">
希望对您有所帮助