CardView

CardView 记录
<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/colorAccent"
        app:cardCornerRadius="5dp"
        app:cardElevation=5dp"
        app:cardMaxElevation="10dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true"
        app:contentPadding="5dp">
        
</android.support.v7.widget.CardView>
  • cardBackgroundColor
  • void setCardBackgroundColor (int color)
设置CardView的背景颜色
  • contentPadding
  • void setContentPadding (int left, int top, int right, int bottom)
设置CardView距内部View的padding,CardView的尺寸不变
  • cardCornerRadius
  • void setRadius (float radius)
设置CardView的圆角半径,在Lollipop前必须大于0
  • cardElevation
  • void setCardElevation (float elevation)
设置CardView的阴影值
  • cardMaxElevation
  • void setMaxCardElevation (float maxElevation)
设置CardView的阴影范围,会腾出空间显示阴影,类似与margin。如果cardUseCompatPadding为false,则在Lollipop+无效
  • cardPreventCornerOverlap
  • void setPreventCornerOverlap (boolean preventCornerOverlap)
On pre-Lollipop platforms, CardView does not clip the bounds of the Card for the rounded corners. 
Instead, it adds padding to content so that it won't overlap with the rounded corners. 
You can disable this behavior by setting this field to false.
在Lollipop之前的平台上,CardView不会为圆角修剪卡片的边界。相反,它会向内容添加填充,以便它不会与圆角重叠。您可以通过将此字段设置为false来禁用此行为。

(Ca 主要适配Lollipop之前的版本)
preventCornerOverlap默认为true,在Lollipop之前设置圆角边缘会是圆角,但是不会修改Card里面的View,并且距内部padding不变,因此为了防止Card圆角与内容重叠Card会扩大,以保证与内部View的边距。preventCornerOverlap为false时,则Card圆角可以想内部缩进,不过内部View(如ImageView)不会变为圆角。
在Lollipop之后CardView圆角就非常友好了,与preventCornerOverlap无关,会将CardView内部View变为圆角。
  • cardUseCompatPadding
  • void setUseCompatPadding (boolean useCompatPadding)
CardView adds additional padding to draw shadows on platforms before Lollipop.

This may cause Cards to have different sizes between Lollipop and before Lollipop.
If you need to align CardView with other Views, you may need api version specific dimension resources to account for the changes. 
As an alternative, you can set this flag to true and CardView will add the same padding values on platforms Lollipop and after.

Since setting this flag to true adds unnecessary gaps in the UI, default value is false.
在Android Lollipop之前,CardView会添加一些额外的padding空间来绘制阴影部分。

这导致了Cards在Lollipop和Lollipop之前具有不同的尺寸。如果需要将CardView与其他View对齐,则可能需要具体的API版本的dimension资源适配。
作为另一种选择,您可以将useCompatPadding设置为true,则CardView将在Lolliop和之后添加相同的padding值。

由于将useCompatPadding设置为true会在UI中添加不必要的空白,因此默认值为false。

(setUseCompatPadding 主要适配Lollipop之后的版本)
大体意思是CardView在Lollipop和Lollipop之前的系统会展示不一样的效果。原因是,在Android Lollipop之前,CardView会添加一些额外的padding空间来绘制阴影部分。这导致了CardView的显示在Lollipop和Lollipop之前具有不同的尺寸。如果不设置cardUseCompatPadding,设置setMaxCardElevation在Lollipop之前,可以有空间显示阴影,在Lollipop之后setMaxCardElevation失效,看不到阴影。在Lollipop之后则需要额外设置一个magrain才能看到阴影。为了解决这个问题,有两种方法:
  1. 使用不同的dimension资源适配,借助values和values-21文件夹中不同的dimens.xml文件,在Lollipop之后为CardView设置margin
  2. 设置useCompatPadding为true,让CardView在不同系统中使用相同的padding值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值