How to set CardView attributes in style.xml file in android

坚强是说给别人听的谎言 提交于 2019-12-10 17:17:04

问题


I have set a style for my CardView in style.xml file but its giving me error on compile time for app:cardCornerRadius, app:cardElevation, app:cardPreventCornerOverlap and app:contentPadding attributes. What is the correct way to set style for a CardView in Android?

Below is some of my code:

<style name="CardViewStyle" parent="CardView">
    <item name="android:layout_marginBottom">@dimen/cardMarginVertical</item>
    <item name="android:layout_marginTop">@dimen/cardMarginVertical</item>
    <item name="android:layout_marginLeft">@dimen/cardMarginHorizontal</item>
    <item name="android:layout_marginRight">@dimen/cardMarginHorizontal</item>
    <item name="app:cardCornerRadius">2dp</item>
    <item name="app:cardElevation">2dp</item>
    <item name="app:cardPreventCornerOverlap">false</item>
    <item name="app:contentPadding">0dp</item>
    <item name="android:layout_width">match_parent</item>
</style>

回答1:


Set parent attribute to CardView. You don't even have to add

  • app: qualifier
  • xmlns:card_view="http://schemas.android.com/apk/res-auto". is not required

Working snippet of code:

<style name="CardViewStyle" parent="CardView">
 <item name="cardCornerRadius">4dp</item>
 <item name="cardElevation">4dp</item>
</style>
  • similar ques
  • How to put a CardView attribute in a style?


来源:https://stackoverflow.com/questions/31221911/how-to-set-cardview-attributes-in-style-xml-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!