有没有办法将主题应用于 Cardview?我不想将样式应用于我正在创建的每个 View 。
这是我的卡片查看 马上:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Custom.Widget.CardView">
<-- I don't want to define the style every time -->
</android.support.v7.widget.CardView>
还有我的样式.xml
<style name="Custom.Widget.CardView" parent="CardView">
<item name="cardBackgroundColor">@color/card_backgroundColor</item>
<item name="cardCornerRadius">12dp</item>
<item name="cardUseCompatPadding">true</item>
<item name="contentPadding">4dp</item>
</style>
我想将样式添加到 主题.xml 这样它将应用于每个 Cardview,但我不知道如何。甚至可以从支持库中获取 View 吗?
当我将以下内容添加到 主题.xml 我收到警告:
no resource found that matches the given name: attr 'Cardview'
<item name="Cardview">@style/Custom.Widget.CardView</item>
最佳答案
有可能,我也花了很长时间才找到。这是一个示例,如果 将橙色背景应用于所有卡片 View 我的风格 整个应用程序都使用样式。
在 主题.xml :
<style name="MyStyle" parent="Theme.AppCompat">
...
<item name="cardViewStyle">@style/MyStyle.Cardview.Orange</item>
</style>
<!-- CardView -->
<style name="MyStyle.Cardview.Orange" parent="CardView">
<item name="cardBackgroundColor">#F3921F</item>
</style>
关于Android CardView 应用主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737423/