【问题标题】:Style's CardBackgroundColor not working on CardView样式的 CardBackgroundColor 在 CardView 上不起作用
【发布时间】:2020-11-18 19:49:13
【问题描述】:

我无法弄清楚为什么我的 CardView 没有获得我的样式设置的背景颜色。根据this answer on another StackOverflow question here using old AndroidSupport-library 您应该可以通过将其添加到默认 AppTheme 来设置它:

<item name="cardBackgroundColor">#ff00ab</item>

但是,这不会以任何方式改变我的颜色。通常通过app:cardBackgroundColor="..." 设置颜色可以正常工作,但对于我的应用程序,我需要它来处理样式。

我还使用一个新项目对其进行了测试,只是添加了使其工作的最少步骤,但它仍然没有样式设置的卡片颜色...

这是我的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="testing string"/>

    </androidx.cardview.widget.CardView>
</RelativeLayout>

在我的styles.xml 中,我已将上面的简单行添加到默认的 Base 应用程序主题中。

感谢您帮助解决我的问题!

编辑: AppTheme 只是上面添加的行的默认 AppTheme。它最终看起来像这样:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="cardBackgroundColor">#ff00ab</item>
</style>

【问题讨论】:

  • 您是否将样式应用于您的 CardView
  • 它正在使用清单中的android:theme 应用于整个活动。将其直接应用到布局中的 CardView 上没有任何区别。 @DebarshiBhattacharjee
  • 你能显示你的应用主题代码吗??
  • @DebarshiBhattacharjee 将其添加到描述中。

标签: android layout android-theme android-styles


【解决方案1】:

对我而言,您发布的内容适用于 API 19 和 22,但不适用于 API 28。以下内容适用于所有 API 级别:

不要在应用的主题中定义cardBackgroundColor,而是定义cardViewStyle。这是应用于所有 CardView 的“默认”样式(除非被布局文件中的 style 属性显式覆盖)。

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

<style name="MyCardViewStyle">
    <item name="cardBackgroundColor">#ff00ab</item>
</style>

您可以通过检查其源代码来确定小部件是否支持默认样式属性。会有一个如下所示的构造函数:

public CardView(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, R.attr.cardViewStyle);
}

这里,第三个参数是R.attr.cardViewStyle,所以我们知道这是我们的主题应该定义的。对于不支持默认样式的组件,第三个参数将改为0

【讨论】:

    【解决方案2】:

    您可以像这样在styles.xml 中创建自己的样式

    <style name="card_view_style">
        <item name="cardBackgroundColor">#ff00ab</item>
    </style>
    

    然后像这样将此样式应用于您的卡片视图

    <androidx.cardview.widget.CardView
        style="@style/card_view_style"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2021-10-01
      • 1970-01-01
      • 2016-09-02
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多