我想使用style.xml文件更改MaterialCardView的背景颜色。这对于在我的Android应用程序中实现“黑暗模式”确实很有帮助。
我已经尝试过动态地做,但是希望使用主题来完成。
为扩展Widget.MaterialComponents.CardView的MaterialCardView创建自定义样式:
<style name="YourCardView" parent="Widget.MaterialComponents.CardView">
<item name="cardBackgroundColor">@color/your_color</item>
</style>
然后,您可以将这种样式手动设置为xml布局中的单个MaterialCardView:
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/YourCardView">
<!-- card content -->
</com.google.android.material.card.MaterialCardView>
或者,您可以为自定义主题(从MaterialComponents扩展主题)中的所有MaterialCardView设置样式:
<style name="YourTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="materialCardViewStyle">@style/YourCardView</item>
</style>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句