【发布时间】: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